12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f9bdedb2SSean MacLennan /*
3f9bdedb2SSean MacLennan * PIKA Warp(tm) board specific routines
4f9bdedb2SSean MacLennan *
5805e324bSSean MacLennan * Copyright (c) 2008-2009 PIKA Technologies
6f9bdedb2SSean MacLennan * Sean MacLennan <smaclennan@pikatech.com>
7f9bdedb2SSean MacLennan */
81892e87aSDmitry Torokhov #include <linux/err.h>
9f9bdedb2SSean MacLennan #include <linux/init.h>
10f9bdedb2SSean MacLennan #include <linux/of_platform.h>
11f9bdedb2SSean MacLennan #include <linux/kthread.h>
121892e87aSDmitry Torokhov #include <linux/leds.h>
134ebef31fSSean MacLennan #include <linux/i2c.h>
144ebef31fSSean MacLennan #include <linux/interrupt.h>
154ebef31fSSean MacLennan #include <linux/delay.h>
16e6f6390aSChristophe Leroy #include <linux/of_address.h>
17e6f6390aSChristophe Leroy #include <linux/of_irq.h>
181892e87aSDmitry Torokhov #include <linux/gpio/consumer.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
2093087948SPaul Gortmaker #include <linux/export.h>
21f9bdedb2SSean MacLennan
22f9bdedb2SSean MacLennan #include <asm/machdep.h>
23f9bdedb2SSean MacLennan #include <asm/udbg.h>
24f9bdedb2SSean MacLennan #include <asm/time.h>
25f9bdedb2SSean MacLennan #include <asm/uic.h>
2693173ce2SStefan Roese #include <asm/ppc4xx.h>
27308c09f1SLaura Abbott #include <asm/dma.h>
28f9bdedb2SSean MacLennan
29805e324bSSean MacLennan
30ce6d73c9SUwe Kleine-König static const struct of_device_id warp_of_bus[] __initconst = {
31f9bdedb2SSean MacLennan { .compatible = "ibm,plb4", },
32f9bdedb2SSean MacLennan { .compatible = "ibm,opb", },
33f9bdedb2SSean MacLennan { .compatible = "ibm,ebc", },
34f9bdedb2SSean MacLennan {},
35f9bdedb2SSean MacLennan };
36f9bdedb2SSean MacLennan
warp_device_probe(void)37f9bdedb2SSean MacLennan static int __init warp_device_probe(void)
38f9bdedb2SSean MacLennan {
39f9bdedb2SSean MacLennan of_platform_bus_probe(NULL, warp_of_bus, NULL);
40f9bdedb2SSean MacLennan return 0;
41f9bdedb2SSean MacLennan }
42f9bdedb2SSean MacLennan machine_device_initcall(warp, warp_device_probe);
43f9bdedb2SSean MacLennan
define_machine(warp)44f9bdedb2SSean MacLennan define_machine(warp) {
45f9bdedb2SSean MacLennan .name = "Warp",
46*1c96fcdeSChristophe Leroy .compatible = "pika,warp",
47f9bdedb2SSean MacLennan .progress = udbg_progress,
48f9bdedb2SSean MacLennan .init_IRQ = uic_init_tree,
49f9bdedb2SSean MacLennan .get_irq = uic_get_irq,
5093173ce2SStefan Roese .restart = ppc4xx_reset_system,
51f9bdedb2SSean MacLennan };
52f9bdedb2SSean MacLennan
53f9bdedb2SSean MacLennan
warp_post_info(void)544ebef31fSSean MacLennan static int __init warp_post_info(void)
55f9bdedb2SSean MacLennan {
56f9bdedb2SSean MacLennan struct device_node *np;
574ebef31fSSean MacLennan void __iomem *fpga;
584ebef31fSSean MacLennan u32 post1, post2;
59f9bdedb2SSean MacLennan
604ebef31fSSean MacLennan /* Sighhhh... POST information is in the sd area. */
614ebef31fSSean MacLennan np = of_find_compatible_node(NULL, NULL, "pika,fpga-sd");
624ebef31fSSean MacLennan if (np == NULL)
634ebef31fSSean MacLennan return -ENOENT;
644ebef31fSSean MacLennan
654ebef31fSSean MacLennan fpga = of_iomap(np, 0);
664ebef31fSSean MacLennan of_node_put(np);
674ebef31fSSean MacLennan if (fpga == NULL)
684ebef31fSSean MacLennan return -ENOENT;
694ebef31fSSean MacLennan
704ebef31fSSean MacLennan post1 = in_be32(fpga + 0x40);
714ebef31fSSean MacLennan post2 = in_be32(fpga + 0x44);
724ebef31fSSean MacLennan
734ebef31fSSean MacLennan iounmap(fpga);
744ebef31fSSean MacLennan
75ba703e1aSSean MacLennan if (post1 || post2)
764ebef31fSSean MacLennan printk(KERN_INFO "Warp POST %08x %08x\n", post1, post2);
77ba703e1aSSean MacLennan else
784ebef31fSSean MacLennan printk(KERN_INFO "Warp POST OK\n");
794ebef31fSSean MacLennan
804ebef31fSSean MacLennan return 0;
814ebef31fSSean MacLennan }
824ebef31fSSean MacLennan
834ebef31fSSean MacLennan
844ebef31fSSean MacLennan #ifdef CONFIG_SENSORS_AD7414
854ebef31fSSean MacLennan
864ebef31fSSean MacLennan static void __iomem *dtm_fpga;
874ebef31fSSean MacLennan
881892e87aSDmitry Torokhov #define WARP_GREEN_LED 0
891892e87aSDmitry Torokhov #define WARP_RED_LED 1
901892e87aSDmitry Torokhov
911892e87aSDmitry Torokhov static struct gpio_led warp_gpio_led_pins[] = {
921892e87aSDmitry Torokhov [WARP_GREEN_LED] = {
931892e87aSDmitry Torokhov .name = "green",
941892e87aSDmitry Torokhov .default_state = LEDS_DEFSTATE_KEEP,
951892e87aSDmitry Torokhov .gpiod = NULL, /* to be filled by pika_setup_leds() */
961892e87aSDmitry Torokhov },
971892e87aSDmitry Torokhov [WARP_RED_LED] = {
981892e87aSDmitry Torokhov .name = "red",
991892e87aSDmitry Torokhov .default_state = LEDS_DEFSTATE_KEEP,
1001892e87aSDmitry Torokhov .gpiod = NULL, /* to be filled by pika_setup_leds() */
1011892e87aSDmitry Torokhov },
1021892e87aSDmitry Torokhov };
1031892e87aSDmitry Torokhov
1041892e87aSDmitry Torokhov static struct gpio_led_platform_data warp_gpio_led_data = {
1051892e87aSDmitry Torokhov .leds = warp_gpio_led_pins,
1061892e87aSDmitry Torokhov .num_leds = ARRAY_SIZE(warp_gpio_led_pins),
1071892e87aSDmitry Torokhov };
1081892e87aSDmitry Torokhov
1091892e87aSDmitry Torokhov static struct platform_device warp_gpio_leds = {
1101892e87aSDmitry Torokhov .name = "leds-gpio",
1111892e87aSDmitry Torokhov .id = -1,
1121892e87aSDmitry Torokhov .dev = {
1131892e87aSDmitry Torokhov .platform_data = &warp_gpio_led_data,
1141892e87aSDmitry Torokhov },
1151892e87aSDmitry Torokhov };
1161892e87aSDmitry Torokhov
temp_isr(int irq,void * context)1174ebef31fSSean MacLennan static irqreturn_t temp_isr(int irq, void *context)
1184ebef31fSSean MacLennan {
119805e324bSSean MacLennan int value = 1;
1204ebef31fSSean MacLennan
1214ebef31fSSean MacLennan local_irq_disable();
1224ebef31fSSean MacLennan
1231892e87aSDmitry Torokhov gpiod_set_value(warp_gpio_led_pins[WARP_GREEN_LED].gpiod, 0);
124805e324bSSean MacLennan
125805e324bSSean MacLennan printk(KERN_EMERG "\n\nCritical Temperature Shutdown\n\n");
1264ebef31fSSean MacLennan
1274ebef31fSSean MacLennan while (1) {
1284ebef31fSSean MacLennan if (dtm_fpga) {
1294ebef31fSSean MacLennan unsigned reset = in_be32(dtm_fpga + 0x14);
1304ebef31fSSean MacLennan out_be32(dtm_fpga + 0x14, reset);
1314ebef31fSSean MacLennan }
1324ebef31fSSean MacLennan
1331892e87aSDmitry Torokhov gpiod_set_value(warp_gpio_led_pins[WARP_RED_LED].gpiod, value);
134805e324bSSean MacLennan value ^= 1;
1354ebef31fSSean MacLennan mdelay(500);
1364ebef31fSSean MacLennan }
137a22ebd06SJosh Boyer
138a22ebd06SJosh Boyer /* Not reached */
139a22ebd06SJosh Boyer return IRQ_HANDLED;
1404ebef31fSSean MacLennan }
1414ebef31fSSean MacLennan
1421892e87aSDmitry Torokhov /*
1431892e87aSDmitry Torokhov * Because green and red power LEDs are normally driven by leds-gpio driver,
1441892e87aSDmitry Torokhov * but in case of critical temperature shutdown we want to drive them
1451892e87aSDmitry Torokhov * ourselves, we acquire both and then create leds-gpio platform device
1461892e87aSDmitry Torokhov * ourselves, instead of doing it through device tree. This way we can still
1471892e87aSDmitry Torokhov * keep access to the gpios and use them when needed.
1481892e87aSDmitry Torokhov */
pika_setup_leds(void)1494ebef31fSSean MacLennan static int pika_setup_leds(void)
1504ebef31fSSean MacLennan {
151805e324bSSean MacLennan struct device_node *np, *child;
1521892e87aSDmitry Torokhov struct gpio_desc *gpio;
1531892e87aSDmitry Torokhov struct gpio_led *led;
1541892e87aSDmitry Torokhov int led_count = 0;
1551892e87aSDmitry Torokhov int error;
1561892e87aSDmitry Torokhov int i;
1574ebef31fSSean MacLennan
1581892e87aSDmitry Torokhov np = of_find_compatible_node(NULL, NULL, "warp-power-leds");
1594ebef31fSSean MacLennan if (!np) {
160805e324bSSean MacLennan printk(KERN_ERR __FILE__ ": Unable to find leds\n");
1614ebef31fSSean MacLennan return -ENOENT;
1624ebef31fSSean MacLennan }
1634ebef31fSSean MacLennan
1641892e87aSDmitry Torokhov for_each_child_of_node(np, child) {
1651892e87aSDmitry Torokhov for (i = 0; i < ARRAY_SIZE(warp_gpio_led_pins); i++) {
1661892e87aSDmitry Torokhov led = &warp_gpio_led_pins[i];
1671892e87aSDmitry Torokhov
1681892e87aSDmitry Torokhov if (!of_node_name_eq(child, led->name))
1691892e87aSDmitry Torokhov continue;
1701892e87aSDmitry Torokhov
1711892e87aSDmitry Torokhov if (led->gpiod) {
1721892e87aSDmitry Torokhov printk(KERN_ERR __FILE__ ": %s led has already been defined\n",
1731892e87aSDmitry Torokhov led->name);
1741892e87aSDmitry Torokhov continue;
1751892e87aSDmitry Torokhov }
1761892e87aSDmitry Torokhov
1771892e87aSDmitry Torokhov gpio = fwnode_gpiod_get_index(of_fwnode_handle(child),
1781892e87aSDmitry Torokhov NULL, 0, GPIOD_ASIS,
1791892e87aSDmitry Torokhov led->name);
1801892e87aSDmitry Torokhov error = PTR_ERR_OR_ZERO(gpio);
1811892e87aSDmitry Torokhov if (error) {
1821892e87aSDmitry Torokhov printk(KERN_ERR __FILE__ ": Failed to get %s led gpio: %d\n",
1831892e87aSDmitry Torokhov led->name, error);
1841892e87aSDmitry Torokhov of_node_put(child);
1851892e87aSDmitry Torokhov goto err_cleanup_pins;
1861892e87aSDmitry Torokhov }
1871892e87aSDmitry Torokhov
1881892e87aSDmitry Torokhov led->gpiod = gpio;
1891892e87aSDmitry Torokhov led_count++;
1901892e87aSDmitry Torokhov }
1911892e87aSDmitry Torokhov }
192805e324bSSean MacLennan
1934ebef31fSSean MacLennan of_node_put(np);
1944ebef31fSSean MacLennan
1951892e87aSDmitry Torokhov /* Skip device registration if no leds have been defined */
1961892e87aSDmitry Torokhov if (led_count) {
1971892e87aSDmitry Torokhov error = platform_device_register(&warp_gpio_leds);
1981892e87aSDmitry Torokhov if (error) {
1991892e87aSDmitry Torokhov printk(KERN_ERR __FILE__ ": Unable to add leds-gpio: %d\n",
2001892e87aSDmitry Torokhov error);
2011892e87aSDmitry Torokhov goto err_cleanup_pins;
2021892e87aSDmitry Torokhov }
2031892e87aSDmitry Torokhov }
2041892e87aSDmitry Torokhov
2054ebef31fSSean MacLennan return 0;
2061892e87aSDmitry Torokhov
2071892e87aSDmitry Torokhov err_cleanup_pins:
2081892e87aSDmitry Torokhov for (i = 0; i < ARRAY_SIZE(warp_gpio_led_pins); i++) {
2091892e87aSDmitry Torokhov led = &warp_gpio_led_pins[i];
2101892e87aSDmitry Torokhov gpiod_put(led->gpiod);
2111892e87aSDmitry Torokhov led->gpiod = NULL;
2121892e87aSDmitry Torokhov }
2131892e87aSDmitry Torokhov return error;
2144ebef31fSSean MacLennan }
2154ebef31fSSean MacLennan
pika_setup_critical_temp(struct device_node * np,struct i2c_client * client)2163984114fSSean MacLennan static void pika_setup_critical_temp(struct device_node *np,
2173984114fSSean MacLennan struct i2c_client *client)
2184ebef31fSSean MacLennan {
2194ebef31fSSean MacLennan int irq, rc;
2204ebef31fSSean MacLennan
2214ebef31fSSean MacLennan /* Do this before enabling critical temp interrupt since we
2224ebef31fSSean MacLennan * may immediately interrupt.
2234ebef31fSSean MacLennan */
2244ebef31fSSean MacLennan pika_setup_leds();
2254ebef31fSSean MacLennan
2264ebef31fSSean MacLennan /* These registers are in 1 degree increments. */
2274ebef31fSSean MacLennan i2c_smbus_write_byte_data(client, 2, 65); /* Thigh */
228048040a3SSean MacLennan i2c_smbus_write_byte_data(client, 3, 0); /* Tlow */
2294ebef31fSSean MacLennan
2304ebef31fSSean MacLennan irq = irq_of_parse_and_map(np, 0);
231ef24ba70SMichael Ellerman if (!irq) {
2324ebef31fSSean MacLennan printk(KERN_ERR __FILE__ ": Unable to get ad7414 irq\n");
2334ebef31fSSean MacLennan return;
2344ebef31fSSean MacLennan }
2354ebef31fSSean MacLennan
2364ebef31fSSean MacLennan rc = request_irq(irq, temp_isr, 0, "ad7414", NULL);
2374ebef31fSSean MacLennan if (rc) {
2384ebef31fSSean MacLennan printk(KERN_ERR __FILE__
2394ebef31fSSean MacLennan ": Unable to request ad7414 irq %d = %d\n", irq, rc);
240f9bdedb2SSean MacLennan return;
241f9bdedb2SSean MacLennan }
242f9bdedb2SSean MacLennan }
243f9bdedb2SSean MacLennan
pika_dtm_check_fan(void __iomem * fpga)2444ebef31fSSean MacLennan static inline void pika_dtm_check_fan(void __iomem *fpga)
2454ebef31fSSean MacLennan {
2464ebef31fSSean MacLennan static int fan_state;
2474ebef31fSSean MacLennan u32 fan = in_be32(fpga + 0x34) & (1 << 14);
248f9bdedb2SSean MacLennan
2494ebef31fSSean MacLennan if (fan_state != fan) {
2504ebef31fSSean MacLennan fan_state = fan;
2514ebef31fSSean MacLennan if (fan)
2524ebef31fSSean MacLennan printk(KERN_WARNING "Fan rotation error detected."
2534ebef31fSSean MacLennan " Please check hardware.\n");
254f9bdedb2SSean MacLennan }
255f9bdedb2SSean MacLennan }
256f9bdedb2SSean MacLennan
pika_dtm_thread(void __iomem * fpga)257f9bdedb2SSean MacLennan static int pika_dtm_thread(void __iomem *fpga)
258f9bdedb2SSean MacLennan {
2593984114fSSean MacLennan struct device_node *np;
2604ebef31fSSean MacLennan struct i2c_client *client;
2614ebef31fSSean MacLennan
2623984114fSSean MacLennan np = of_find_compatible_node(NULL, NULL, "adi,ad7414");
2633984114fSSean MacLennan if (np == NULL)
2643984114fSSean MacLennan return -ENOENT;
2653984114fSSean MacLennan
2663984114fSSean MacLennan client = of_find_i2c_device_by_node(np);
2673984114fSSean MacLennan if (client == NULL) {
2683984114fSSean MacLennan of_node_put(np);
2693984114fSSean MacLennan return -ENOENT;
2704ebef31fSSean MacLennan }
2714ebef31fSSean MacLennan
2723984114fSSean MacLennan pika_setup_critical_temp(np, client);
2734ebef31fSSean MacLennan
2743984114fSSean MacLennan of_node_put(np);
2754ebef31fSSean MacLennan
2763984114fSSean MacLennan printk(KERN_INFO "Warp DTM thread running.\n");
277f9bdedb2SSean MacLennan
278f9bdedb2SSean MacLennan while (!kthread_should_stop()) {
279048040a3SSean MacLennan int val;
280048040a3SSean MacLennan
281048040a3SSean MacLennan val = i2c_smbus_read_word_data(client, 0);
282048040a3SSean MacLennan if (val < 0)
283048040a3SSean MacLennan dev_dbg(&client->dev, "DTM read temp failed.\n");
284048040a3SSean MacLennan else {
285048040a3SSean MacLennan s16 temp = swab16(val);
2864ebef31fSSean MacLennan out_be32(fpga + 0x20, temp);
287048040a3SSean MacLennan }
288f9bdedb2SSean MacLennan
2894ebef31fSSean MacLennan pika_dtm_check_fan(fpga);
290f9bdedb2SSean MacLennan
291f9bdedb2SSean MacLennan set_current_state(TASK_INTERRUPTIBLE);
292f9bdedb2SSean MacLennan schedule_timeout(HZ);
293f9bdedb2SSean MacLennan }
294f9bdedb2SSean MacLennan
295f9bdedb2SSean MacLennan return 0;
296f9bdedb2SSean MacLennan }
297f9bdedb2SSean MacLennan
pika_dtm_start(void)298f9bdedb2SSean MacLennan static int __init pika_dtm_start(void)
299f9bdedb2SSean MacLennan {
300f9bdedb2SSean MacLennan struct task_struct *dtm_thread;
301f9bdedb2SSean MacLennan struct device_node *np;
302f9bdedb2SSean MacLennan
303f9bdedb2SSean MacLennan np = of_find_compatible_node(NULL, NULL, "pika,fpga");
304f9bdedb2SSean MacLennan if (np == NULL)
305f9bdedb2SSean MacLennan return -ENOENT;
306f9bdedb2SSean MacLennan
3074ebef31fSSean MacLennan dtm_fpga = of_iomap(np, 0);
308f9bdedb2SSean MacLennan of_node_put(np);
3094ebef31fSSean MacLennan if (dtm_fpga == NULL)
310f9bdedb2SSean MacLennan return -ENOENT;
311f9bdedb2SSean MacLennan
312805e324bSSean MacLennan /* Must get post info before thread starts. */
313805e324bSSean MacLennan warp_post_info();
314805e324bSSean MacLennan
3154ebef31fSSean MacLennan dtm_thread = kthread_run(pika_dtm_thread, dtm_fpga, "pika-dtm");
316f9bdedb2SSean MacLennan if (IS_ERR(dtm_thread)) {
3174ebef31fSSean MacLennan iounmap(dtm_fpga);
318f9bdedb2SSean MacLennan return PTR_ERR(dtm_thread);
319f9bdedb2SSean MacLennan }
320f9bdedb2SSean MacLennan
321f9bdedb2SSean MacLennan return 0;
322f9bdedb2SSean MacLennan }
3234ebef31fSSean MacLennan machine_late_initcall(warp, pika_dtm_start);
3244ebef31fSSean MacLennan
3254ebef31fSSean MacLennan #else /* !CONFIG_SENSORS_AD7414 */
3264ebef31fSSean MacLennan
327805e324bSSean MacLennan machine_late_initcall(warp, warp_post_info);
328805e324bSSean MacLennan
329f9bdedb2SSean MacLennan #endif
330