neponset.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/arm/mach-sa1100/neponset.c
  4. */
  5. #include <linux/err.h>
  6. #include <linux/gpio/driver.h>
  7. #include <linux/gpio/gpio-reg.h>
  8. #include <linux/init.h>
  9. #include <linux/ioport.h>
  10. #include <linux/irq.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_data/sa11x0-serial.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm.h>
  16. #include <linux/serial_core.h>
  17. #include <linux/slab.h>
  18. #include <linux/smc91x.h>
  19. #include <asm/mach-types.h>
  20. #include <asm/mach/map.h>
  21. #include <asm/hardware/sa1111.h>
  22. #include <asm/sizes.h>
  23. #include <mach/hardware.h>
  24. #include <mach/assabet.h>
  25. #include <mach/neponset.h>
  26. #include <mach/irqs.h>
  27. #define NEP_IRQ_SMC91X 0
  28. #define NEP_IRQ_USAR 1
  29. #define NEP_IRQ_SA1111 2
  30. #define NEP_IRQ_NR 3
  31. #define WHOAMI 0x00
  32. #define LEDS 0x10
  33. #define SWPK 0x20
  34. #define IRR 0x24
  35. #define KP_Y_IN 0x80
  36. #define KP_X_OUT 0x90
  37. #define NCR_0 0xa0
  38. #define MDM_CTL_0 0xb0
  39. #define MDM_CTL_1 0xb4
  40. #define AUD_CTL 0xc0
  41. #define IRR_ETHERNET (1 << 0)
  42. #define IRR_USAR (1 << 1)
  43. #define IRR_SA1111 (1 << 2)
  44. #define NCR_NGPIO 7
  45. #define MDM_CTL0_RTS1 (1 << 0)
  46. #define MDM_CTL0_DTR1 (1 << 1)
  47. #define MDM_CTL0_RTS2 (1 << 2)
  48. #define MDM_CTL0_DTR2 (1 << 3)
  49. #define MDM_CTL0_NGPIO 4
  50. #define MDM_CTL1_CTS1 (1 << 0)
  51. #define MDM_CTL1_DSR1 (1 << 1)
  52. #define MDM_CTL1_DCD1 (1 << 2)
  53. #define MDM_CTL1_CTS2 (1 << 3)
  54. #define MDM_CTL1_DSR2 (1 << 4)
  55. #define MDM_CTL1_DCD2 (1 << 5)
  56. #define MDM_CTL1_NGPIO 6
  57. #define AUD_SEL_1341 (1 << 0)
  58. #define AUD_MUTE_1341 (1 << 1)
  59. #define AUD_NGPIO 2
  60. extern void sa1110_mb_disable(void);
  61. #define to_neponset_gpio_chip(x) container_of(x, struct neponset_gpio_chip, gc)
  62. static const char *neponset_ncr_names[] = {
  63. "gp01_off", "tp_power", "ms_power", "enet_osc",
  64. "spi_kb_wk_up", "a0vpp", "a1vpp"
  65. };
  66. static const char *neponset_mdmctl0_names[] = {
  67. "rts3", "dtr3", "rts1", "dtr1",
  68. };
  69. static const char *neponset_mdmctl1_names[] = {
  70. "cts3", "dsr3", "dcd3", "cts1", "dsr1", "dcd1"
  71. };
  72. static const char *neponset_aud_names[] = {
  73. "sel_1341", "mute_1341",
  74. };
  75. struct neponset_drvdata {
  76. void __iomem *base;
  77. struct platform_device *sa1111;
  78. struct platform_device *smc91x;
  79. unsigned irq_base;
  80. struct gpio_chip *gpio[4];
  81. };
  82. static struct neponset_drvdata *nep;
  83. void neponset_ncr_frob(unsigned int mask, unsigned int val)
  84. {
  85. struct neponset_drvdata *n = nep;
  86. unsigned long m = mask, v = val;
  87. if (nep)
  88. n->gpio[0]->set_multiple(n->gpio[0], &m, &v);
  89. else
  90. WARN(1, "nep unset\n");
  91. }
  92. EXPORT_SYMBOL(neponset_ncr_frob);
  93. static void neponset_set_mctrl(struct uart_port *port, u_int mctrl)
  94. {
  95. struct neponset_drvdata *n = nep;
  96. unsigned long mask, val = 0;
  97. if (!n)
  98. return;
  99. if (port->mapbase == _Ser1UTCR0) {
  100. mask = MDM_CTL0_RTS2 | MDM_CTL0_DTR2;
  101. if (!(mctrl & TIOCM_RTS))
  102. val |= MDM_CTL0_RTS2;
  103. if (!(mctrl & TIOCM_DTR))
  104. val |= MDM_CTL0_DTR2;
  105. } else if (port->mapbase == _Ser3UTCR0) {
  106. mask = MDM_CTL0_RTS1 | MDM_CTL0_DTR1;
  107. if (!(mctrl & TIOCM_RTS))
  108. val |= MDM_CTL0_RTS1;
  109. if (!(mctrl & TIOCM_DTR))
  110. val |= MDM_CTL0_DTR1;
  111. }
  112. n->gpio[1]->set_multiple(n->gpio[1], &mask, &val);
  113. }
  114. static u_int neponset_get_mctrl(struct uart_port *port)
  115. {
  116. void __iomem *base = nep->base;
  117. u_int ret = TIOCM_CD | TIOCM_CTS | TIOCM_DSR;
  118. u_int mdm_ctl1;
  119. if (!base)
  120. return ret;
  121. mdm_ctl1 = readb_relaxed(base + MDM_CTL_1);
  122. if (port->mapbase == _Ser1UTCR0) {
  123. if (mdm_ctl1 & MDM_CTL1_DCD2)
  124. ret &= ~TIOCM_CD;
  125. if (mdm_ctl1 & MDM_CTL1_CTS2)
  126. ret &= ~TIOCM_CTS;
  127. if (mdm_ctl1 & MDM_CTL1_DSR2)
  128. ret &= ~TIOCM_DSR;
  129. } else if (port->mapbase == _Ser3UTCR0) {
  130. if (mdm_ctl1 & MDM_CTL1_DCD1)
  131. ret &= ~TIOCM_CD;
  132. if (mdm_ctl1 & MDM_CTL1_CTS1)
  133. ret &= ~TIOCM_CTS;
  134. if (mdm_ctl1 & MDM_CTL1_DSR1)
  135. ret &= ~TIOCM_DSR;
  136. }
  137. return ret;
  138. }
  139. static struct sa1100_port_fns neponset_port_fns = {
  140. .set_mctrl = neponset_set_mctrl,
  141. .get_mctrl = neponset_get_mctrl,
  142. };
  143. /*
  144. * Install handler for Neponset IRQ. Note that we have to loop here
  145. * since the ETHERNET and USAR IRQs are level based, and we need to
  146. * ensure that the IRQ signal is deasserted before returning. This
  147. * is rather unfortunate.
  148. */
  149. static void neponset_irq_handler(struct irq_desc *desc)
  150. {
  151. struct neponset_drvdata *d = irq_desc_get_handler_data(desc);
  152. unsigned int irr;
  153. while (1) {
  154. /*
  155. * Acknowledge the parent IRQ.
  156. */
  157. desc->irq_data.chip->irq_ack(&desc->irq_data);
  158. /*
  159. * Read the interrupt reason register. Let's have all
  160. * active IRQ bits high. Note: there is a typo in the
  161. * Neponset user's guide for the SA1111 IRR level.
  162. */
  163. irr = readb_relaxed(d->base + IRR);
  164. irr ^= IRR_ETHERNET | IRR_USAR;
  165. if ((irr & (IRR_ETHERNET | IRR_USAR | IRR_SA1111)) == 0)
  166. break;
  167. /*
  168. * Since there is no individual mask, we have to
  169. * mask the parent IRQ. This is safe, since we'll
  170. * recheck the register for any pending IRQs.
  171. */
  172. if (irr & (IRR_ETHERNET | IRR_USAR)) {
  173. desc->irq_data.chip->irq_mask(&desc->irq_data);
  174. /*
  175. * Ack the interrupt now to prevent re-entering
  176. * this neponset handler. Again, this is safe
  177. * since we'll check the IRR register prior to
  178. * leaving.
  179. */
  180. desc->irq_data.chip->irq_ack(&desc->irq_data);
  181. if (irr & IRR_ETHERNET)
  182. generic_handle_irq(d->irq_base + NEP_IRQ_SMC91X);
  183. if (irr & IRR_USAR)
  184. generic_handle_irq(d->irq_base + NEP_IRQ_USAR);
  185. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  186. }
  187. if (irr & IRR_SA1111)
  188. generic_handle_irq(d->irq_base + NEP_IRQ_SA1111);
  189. }
  190. }
  191. /* Yes, we really do not have any kind of masking or unmasking */
  192. static void nochip_noop(struct irq_data *irq)
  193. {
  194. }
  195. static struct irq_chip nochip = {
  196. .name = "neponset",
  197. .irq_ack = nochip_noop,
  198. .irq_mask = nochip_noop,
  199. .irq_unmask = nochip_noop,
  200. };
  201. static int neponset_init_gpio(struct gpio_chip **gcp,
  202. struct device *dev, const char *label, void __iomem *reg,
  203. unsigned num, bool in, const char *const * names)
  204. {
  205. struct gpio_chip *gc;
  206. gc = gpio_reg_init(dev, reg, -1, num, label, in ? 0xffffffff : 0,
  207. readl_relaxed(reg), names, NULL, NULL);
  208. if (IS_ERR(gc))
  209. return PTR_ERR(gc);
  210. *gcp = gc;
  211. return 0;
  212. }
  213. static struct sa1111_platform_data sa1111_info = {
  214. .disable_devs = SA1111_DEVID_PS2_MSE,
  215. };
  216. static int neponset_probe(struct platform_device *dev)
  217. {
  218. struct neponset_drvdata *d;
  219. struct resource *nep_res, *sa1111_res, *smc91x_res;
  220. struct resource sa1111_resources[] = {
  221. DEFINE_RES_MEM(0x40000000, SZ_8K),
  222. { .flags = IORESOURCE_IRQ },
  223. };
  224. struct platform_device_info sa1111_devinfo = {
  225. .parent = &dev->dev,
  226. .name = "sa1111",
  227. .id = 0,
  228. .res = sa1111_resources,
  229. .num_res = ARRAY_SIZE(sa1111_resources),
  230. .data = &sa1111_info,
  231. .size_data = sizeof(sa1111_info),
  232. .dma_mask = 0xffffffffUL,
  233. };
  234. struct resource smc91x_resources[] = {
  235. DEFINE_RES_MEM_NAMED(SA1100_CS3_PHYS,
  236. 0x02000000, "smc91x-regs"),
  237. DEFINE_RES_MEM_NAMED(SA1100_CS3_PHYS + 0x02000000,
  238. 0x02000000, "smc91x-attrib"),
  239. { .flags = IORESOURCE_IRQ },
  240. };
  241. struct smc91x_platdata smc91x_platdata = {
  242. .flags = SMC91X_USE_8BIT | SMC91X_IO_SHIFT_2 | SMC91X_NOWAIT,
  243. };
  244. struct platform_device_info smc91x_devinfo = {
  245. .parent = &dev->dev,
  246. .name = "smc91x",
  247. .id = 0,
  248. .res = smc91x_resources,
  249. .num_res = ARRAY_SIZE(smc91x_resources),
  250. .data = &smc91x_platdata,
  251. .size_data = sizeof(smc91x_platdata),
  252. };
  253. int ret, irq;
  254. if (nep)
  255. return -EBUSY;
  256. irq = ret = platform_get_irq(dev, 0);
  257. if (ret < 0)
  258. goto err_alloc;
  259. nep_res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  260. smc91x_res = platform_get_resource(dev, IORESOURCE_MEM, 1);
  261. sa1111_res = platform_get_resource(dev, IORESOURCE_MEM, 2);
  262. if (!nep_res || !smc91x_res || !sa1111_res) {
  263. ret = -ENXIO;
  264. goto err_alloc;
  265. }
  266. d = kzalloc(sizeof(*d), GFP_KERNEL);
  267. if (!d) {
  268. ret = -ENOMEM;
  269. goto err_alloc;
  270. }
  271. d->base = ioremap(nep_res->start, SZ_4K);
  272. if (!d->base) {
  273. ret = -ENOMEM;
  274. goto err_ioremap;
  275. }
  276. if (readb_relaxed(d->base + WHOAMI) != 0x11) {
  277. dev_warn(&dev->dev, "Neponset board detected, but wrong ID: %02x\n",
  278. readb_relaxed(d->base + WHOAMI));
  279. ret = -ENODEV;
  280. goto err_id;
  281. }
  282. ret = irq_alloc_descs(-1, IRQ_BOARD_START, NEP_IRQ_NR, -1);
  283. if (ret <= 0) {
  284. dev_err(&dev->dev, "unable to allocate %u irqs: %d\n",
  285. NEP_IRQ_NR, ret);
  286. if (ret == 0)
  287. ret = -ENOMEM;
  288. goto err_irq_alloc;
  289. }
  290. d->irq_base = ret;
  291. irq_set_chip_and_handler(d->irq_base + NEP_IRQ_SMC91X, &nochip,
  292. handle_simple_irq);
  293. irq_clear_status_flags(d->irq_base + NEP_IRQ_SMC91X, IRQ_NOREQUEST | IRQ_NOPROBE);
  294. irq_set_chip_and_handler(d->irq_base + NEP_IRQ_USAR, &nochip,
  295. handle_simple_irq);
  296. irq_clear_status_flags(d->irq_base + NEP_IRQ_USAR, IRQ_NOREQUEST | IRQ_NOPROBE);
  297. irq_set_chip(d->irq_base + NEP_IRQ_SA1111, &nochip);
  298. irq_set_irq_type(irq, IRQ_TYPE_EDGE_RISING);
  299. irq_set_chained_handler_and_data(irq, neponset_irq_handler, d);
  300. /* Disable GPIO 0/1 drivers so the buttons work on the Assabet */
  301. writeb_relaxed(NCR_GP01_OFF, d->base + NCR_0);
  302. neponset_init_gpio(&d->gpio[0], &dev->dev, "neponset-ncr",
  303. d->base + NCR_0, NCR_NGPIO, false,
  304. neponset_ncr_names);
  305. neponset_init_gpio(&d->gpio[1], &dev->dev, "neponset-mdm-ctl0",
  306. d->base + MDM_CTL_0, MDM_CTL0_NGPIO, false,
  307. neponset_mdmctl0_names);
  308. neponset_init_gpio(&d->gpio[2], &dev->dev, "neponset-mdm-ctl1",
  309. d->base + MDM_CTL_1, MDM_CTL1_NGPIO, true,
  310. neponset_mdmctl1_names);
  311. neponset_init_gpio(&d->gpio[3], &dev->dev, "neponset-aud-ctl",
  312. d->base + AUD_CTL, AUD_NGPIO, false,
  313. neponset_aud_names);
  314. /*
  315. * We would set IRQ_GPIO25 to be a wake-up IRQ, but unfortunately
  316. * something on the Neponset activates this IRQ on sleep (eth?)
  317. */
  318. #if 0
  319. enable_irq_wake(irq);
  320. #endif
  321. dev_info(&dev->dev, "Neponset daughter board, providing IRQ%u-%u\n",
  322. d->irq_base, d->irq_base + NEP_IRQ_NR - 1);
  323. nep = d;
  324. sa1100_register_uart_fns(&neponset_port_fns);
  325. /* Ensure that the memory bus request/grant signals are setup */
  326. sa1110_mb_disable();
  327. sa1111_resources[0].parent = sa1111_res;
  328. sa1111_resources[1].start = d->irq_base + NEP_IRQ_SA1111;
  329. sa1111_resources[1].end = d->irq_base + NEP_IRQ_SA1111;
  330. d->sa1111 = platform_device_register_full(&sa1111_devinfo);
  331. smc91x_resources[0].parent = smc91x_res;
  332. smc91x_resources[1].parent = smc91x_res;
  333. smc91x_resources[2].start = d->irq_base + NEP_IRQ_SMC91X;
  334. smc91x_resources[2].end = d->irq_base + NEP_IRQ_SMC91X;
  335. d->smc91x = platform_device_register_full(&smc91x_devinfo);
  336. platform_set_drvdata(dev, d);
  337. return 0;
  338. err_irq_alloc:
  339. err_id:
  340. iounmap(d->base);
  341. err_ioremap:
  342. kfree(d);
  343. err_alloc:
  344. return ret;
  345. }
  346. static int neponset_remove(struct platform_device *dev)
  347. {
  348. struct neponset_drvdata *d = platform_get_drvdata(dev);
  349. int irq = platform_get_irq(dev, 0);
  350. if (!IS_ERR(d->sa1111))
  351. platform_device_unregister(d->sa1111);
  352. if (!IS_ERR(d->smc91x))
  353. platform_device_unregister(d->smc91x);
  354. irq_set_chained_handler(irq, NULL);
  355. irq_free_descs(d->irq_base, NEP_IRQ_NR);
  356. nep = NULL;
  357. iounmap(d->base);
  358. kfree(d);
  359. return 0;
  360. }
  361. #ifdef CONFIG_PM_SLEEP
  362. static int neponset_resume(struct device *dev)
  363. {
  364. struct neponset_drvdata *d = dev_get_drvdata(dev);
  365. int i, ret = 0;
  366. for (i = 0; i < ARRAY_SIZE(d->gpio); i++) {
  367. ret = gpio_reg_resume(d->gpio[i]);
  368. if (ret)
  369. break;
  370. }
  371. return ret;
  372. }
  373. static const struct dev_pm_ops neponset_pm_ops = {
  374. .resume_noirq = neponset_resume,
  375. .restore_noirq = neponset_resume,
  376. };
  377. #define PM_OPS &neponset_pm_ops
  378. #else
  379. #define PM_OPS NULL
  380. #endif
  381. static struct platform_driver neponset_device_driver = {
  382. .probe = neponset_probe,
  383. .remove = neponset_remove,
  384. .driver = {
  385. .name = "neponset",
  386. .pm = PM_OPS,
  387. },
  388. };
  389. static int __init neponset_init(void)
  390. {
  391. return platform_driver_register(&neponset_device_driver);
  392. }
  393. subsys_initcall(neponset_init);