parport_ax88796.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* linux/drivers/parport/parport_ax88796.c
  2. *
  3. * (c) 2005,2006 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/parport.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/errno.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <asm/io.h>
  19. #include <asm/irq.h>
  20. #define AX_SPR_BUSY (1<<7)
  21. #define AX_SPR_ACK (1<<6)
  22. #define AX_SPR_PE (1<<5)
  23. #define AX_SPR_SLCT (1<<4)
  24. #define AX_SPR_ERR (1<<3)
  25. #define AX_CPR_nDOE (1<<5)
  26. #define AX_CPR_SLCTIN (1<<3)
  27. #define AX_CPR_nINIT (1<<2)
  28. #define AX_CPR_ATFD (1<<1)
  29. #define AX_CPR_STRB (1<<0)
  30. struct ax_drvdata {
  31. struct parport *parport;
  32. struct parport_state suspend;
  33. struct device *dev;
  34. struct resource *io;
  35. unsigned char irq_enabled;
  36. void __iomem *base;
  37. void __iomem *spp_data;
  38. void __iomem *spp_spr;
  39. void __iomem *spp_cpr;
  40. };
  41. static inline struct ax_drvdata *pp_to_drv(struct parport *p)
  42. {
  43. return p->private_data;
  44. }
  45. static unsigned char
  46. parport_ax88796_read_data(struct parport *p)
  47. {
  48. struct ax_drvdata *dd = pp_to_drv(p);
  49. return readb(dd->spp_data);
  50. }
  51. static void
  52. parport_ax88796_write_data(struct parport *p, unsigned char data)
  53. {
  54. struct ax_drvdata *dd = pp_to_drv(p);
  55. writeb(data, dd->spp_data);
  56. }
  57. static unsigned char
  58. parport_ax88796_read_control(struct parport *p)
  59. {
  60. struct ax_drvdata *dd = pp_to_drv(p);
  61. unsigned int cpr = readb(dd->spp_cpr);
  62. unsigned int ret = 0;
  63. if (!(cpr & AX_CPR_STRB))
  64. ret |= PARPORT_CONTROL_STROBE;
  65. if (!(cpr & AX_CPR_ATFD))
  66. ret |= PARPORT_CONTROL_AUTOFD;
  67. if (cpr & AX_CPR_nINIT)
  68. ret |= PARPORT_CONTROL_INIT;
  69. if (!(cpr & AX_CPR_SLCTIN))
  70. ret |= PARPORT_CONTROL_SELECT;
  71. return ret;
  72. }
  73. static void
  74. parport_ax88796_write_control(struct parport *p, unsigned char control)
  75. {
  76. struct ax_drvdata *dd = pp_to_drv(p);
  77. unsigned int cpr = readb(dd->spp_cpr);
  78. cpr &= AX_CPR_nDOE;
  79. if (!(control & PARPORT_CONTROL_STROBE))
  80. cpr |= AX_CPR_STRB;
  81. if (!(control & PARPORT_CONTROL_AUTOFD))
  82. cpr |= AX_CPR_ATFD;
  83. if (control & PARPORT_CONTROL_INIT)
  84. cpr |= AX_CPR_nINIT;
  85. if (!(control & PARPORT_CONTROL_SELECT))
  86. cpr |= AX_CPR_SLCTIN;
  87. dev_dbg(dd->dev, "write_control: ctrl=%02x, cpr=%02x\n", control, cpr);
  88. writeb(cpr, dd->spp_cpr);
  89. if (parport_ax88796_read_control(p) != control) {
  90. dev_err(dd->dev, "write_control: read != set (%02x, %02x)\n",
  91. parport_ax88796_read_control(p), control);
  92. }
  93. }
  94. static unsigned char
  95. parport_ax88796_read_status(struct parport *p)
  96. {
  97. struct ax_drvdata *dd = pp_to_drv(p);
  98. unsigned int status = readb(dd->spp_spr);
  99. unsigned int ret = 0;
  100. if (status & AX_SPR_BUSY)
  101. ret |= PARPORT_STATUS_BUSY;
  102. if (status & AX_SPR_ACK)
  103. ret |= PARPORT_STATUS_ACK;
  104. if (status & AX_SPR_ERR)
  105. ret |= PARPORT_STATUS_ERROR;
  106. if (status & AX_SPR_SLCT)
  107. ret |= PARPORT_STATUS_SELECT;
  108. if (status & AX_SPR_PE)
  109. ret |= PARPORT_STATUS_PAPEROUT;
  110. return ret;
  111. }
  112. static unsigned char
  113. parport_ax88796_frob_control(struct parport *p, unsigned char mask,
  114. unsigned char val)
  115. {
  116. struct ax_drvdata *dd = pp_to_drv(p);
  117. unsigned char old = parport_ax88796_read_control(p);
  118. dev_dbg(dd->dev, "frob: mask=%02x, val=%02x, old=%02x\n",
  119. mask, val, old);
  120. parport_ax88796_write_control(p, (old & ~mask) | val);
  121. return old;
  122. }
  123. static void
  124. parport_ax88796_enable_irq(struct parport *p)
  125. {
  126. struct ax_drvdata *dd = pp_to_drv(p);
  127. unsigned long flags;
  128. local_irq_save(flags);
  129. if (!dd->irq_enabled) {
  130. enable_irq(p->irq);
  131. dd->irq_enabled = 1;
  132. }
  133. local_irq_restore(flags);
  134. }
  135. static void
  136. parport_ax88796_disable_irq(struct parport *p)
  137. {
  138. struct ax_drvdata *dd = pp_to_drv(p);
  139. unsigned long flags;
  140. local_irq_save(flags);
  141. if (dd->irq_enabled) {
  142. disable_irq(p->irq);
  143. dd->irq_enabled = 0;
  144. }
  145. local_irq_restore(flags);
  146. }
  147. static void
  148. parport_ax88796_data_forward(struct parport *p)
  149. {
  150. struct ax_drvdata *dd = pp_to_drv(p);
  151. void __iomem *cpr = dd->spp_cpr;
  152. writeb((readb(cpr) & ~AX_CPR_nDOE), cpr);
  153. }
  154. static void
  155. parport_ax88796_data_reverse(struct parport *p)
  156. {
  157. struct ax_drvdata *dd = pp_to_drv(p);
  158. void __iomem *cpr = dd->spp_cpr;
  159. writeb(readb(cpr) | AX_CPR_nDOE, cpr);
  160. }
  161. static void
  162. parport_ax88796_init_state(struct pardevice *d, struct parport_state *s)
  163. {
  164. struct ax_drvdata *dd = pp_to_drv(d->port);
  165. memset(s, 0, sizeof(struct parport_state));
  166. dev_dbg(dd->dev, "init_state: %p: state=%p\n", d, s);
  167. s->u.ax88796.cpr = readb(dd->spp_cpr);
  168. }
  169. static void
  170. parport_ax88796_save_state(struct parport *p, struct parport_state *s)
  171. {
  172. struct ax_drvdata *dd = pp_to_drv(p);
  173. dev_dbg(dd->dev, "save_state: %p: state=%p\n", p, s);
  174. s->u.ax88796.cpr = readb(dd->spp_cpr);
  175. }
  176. static void
  177. parport_ax88796_restore_state(struct parport *p, struct parport_state *s)
  178. {
  179. struct ax_drvdata *dd = pp_to_drv(p);
  180. dev_dbg(dd->dev, "restore_state: %p: state=%p\n", p, s);
  181. writeb(s->u.ax88796.cpr, dd->spp_cpr);
  182. }
  183. static struct parport_operations parport_ax88796_ops = {
  184. .write_data = parport_ax88796_write_data,
  185. .read_data = parport_ax88796_read_data,
  186. .write_control = parport_ax88796_write_control,
  187. .read_control = parport_ax88796_read_control,
  188. .frob_control = parport_ax88796_frob_control,
  189. .read_status = parport_ax88796_read_status,
  190. .enable_irq = parport_ax88796_enable_irq,
  191. .disable_irq = parport_ax88796_disable_irq,
  192. .data_forward = parport_ax88796_data_forward,
  193. .data_reverse = parport_ax88796_data_reverse,
  194. .init_state = parport_ax88796_init_state,
  195. .save_state = parport_ax88796_save_state,
  196. .restore_state = parport_ax88796_restore_state,
  197. .epp_write_data = parport_ieee1284_epp_write_data,
  198. .epp_read_data = parport_ieee1284_epp_read_data,
  199. .epp_write_addr = parport_ieee1284_epp_write_addr,
  200. .epp_read_addr = parport_ieee1284_epp_read_addr,
  201. .ecp_write_data = parport_ieee1284_ecp_write_data,
  202. .ecp_read_data = parport_ieee1284_ecp_read_data,
  203. .ecp_write_addr = parport_ieee1284_ecp_write_addr,
  204. .compat_write_data = parport_ieee1284_write_compat,
  205. .nibble_read_data = parport_ieee1284_read_nibble,
  206. .byte_read_data = parport_ieee1284_read_byte,
  207. .owner = THIS_MODULE,
  208. };
  209. static int parport_ax88796_probe(struct platform_device *pdev)
  210. {
  211. struct device *_dev = &pdev->dev;
  212. struct ax_drvdata *dd;
  213. struct parport *pp = NULL;
  214. struct resource *res;
  215. unsigned long size;
  216. int spacing;
  217. int irq;
  218. int ret;
  219. dd = kzalloc(sizeof(struct ax_drvdata), GFP_KERNEL);
  220. if (dd == NULL) {
  221. dev_err(_dev, "no memory for private data\n");
  222. return -ENOMEM;
  223. }
  224. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  225. if (res == NULL) {
  226. dev_err(_dev, "no MEM specified\n");
  227. ret = -ENXIO;
  228. goto exit_mem;
  229. }
  230. size = resource_size(res);
  231. spacing = size / 3;
  232. dd->io = request_mem_region(res->start, size, pdev->name);
  233. if (dd->io == NULL) {
  234. dev_err(_dev, "cannot reserve memory\n");
  235. ret = -ENXIO;
  236. goto exit_mem;
  237. }
  238. dd->base = ioremap(res->start, size);
  239. if (dd->base == NULL) {
  240. dev_err(_dev, "cannot ioremap region\n");
  241. ret = -ENXIO;
  242. goto exit_res;
  243. }
  244. irq = platform_get_irq(pdev, 0);
  245. if (irq <= 0)
  246. irq = PARPORT_IRQ_NONE;
  247. pp = parport_register_port((unsigned long)dd->base, irq,
  248. PARPORT_DMA_NONE,
  249. &parport_ax88796_ops);
  250. if (pp == NULL) {
  251. dev_err(_dev, "failed to register parallel port\n");
  252. ret = -ENOMEM;
  253. goto exit_unmap;
  254. }
  255. pp->private_data = dd;
  256. dd->parport = pp;
  257. dd->dev = _dev;
  258. dd->spp_data = dd->base;
  259. dd->spp_spr = dd->base + (spacing * 1);
  260. dd->spp_cpr = dd->base + (spacing * 2);
  261. /* initialise the port controls */
  262. writeb(AX_CPR_STRB, dd->spp_cpr);
  263. if (irq >= 0) {
  264. /* request irq */
  265. ret = request_irq(irq, parport_irq_handler,
  266. IRQF_TRIGGER_FALLING, pdev->name, pp);
  267. if (ret < 0)
  268. goto exit_port;
  269. dd->irq_enabled = 1;
  270. }
  271. platform_set_drvdata(pdev, pp);
  272. dev_info(_dev, "attached parallel port driver\n");
  273. parport_announce_port(pp);
  274. return 0;
  275. exit_port:
  276. parport_remove_port(pp);
  277. exit_unmap:
  278. iounmap(dd->base);
  279. exit_res:
  280. release_resource(dd->io);
  281. kfree(dd->io);
  282. exit_mem:
  283. kfree(dd);
  284. return ret;
  285. }
  286. static int parport_ax88796_remove(struct platform_device *pdev)
  287. {
  288. struct parport *p = platform_get_drvdata(pdev);
  289. struct ax_drvdata *dd = pp_to_drv(p);
  290. free_irq(p->irq, p);
  291. parport_remove_port(p);
  292. iounmap(dd->base);
  293. release_resource(dd->io);
  294. kfree(dd->io);
  295. kfree(dd);
  296. return 0;
  297. }
  298. #ifdef CONFIG_PM
  299. static int parport_ax88796_suspend(struct platform_device *dev,
  300. pm_message_t state)
  301. {
  302. struct parport *p = platform_get_drvdata(dev);
  303. struct ax_drvdata *dd = pp_to_drv(p);
  304. parport_ax88796_save_state(p, &dd->suspend);
  305. writeb(AX_CPR_nDOE | AX_CPR_STRB, dd->spp_cpr);
  306. return 0;
  307. }
  308. static int parport_ax88796_resume(struct platform_device *dev)
  309. {
  310. struct parport *p = platform_get_drvdata(dev);
  311. struct ax_drvdata *dd = pp_to_drv(p);
  312. parport_ax88796_restore_state(p, &dd->suspend);
  313. return 0;
  314. }
  315. #else
  316. #define parport_ax88796_suspend NULL
  317. #define parport_ax88796_resume NULL
  318. #endif
  319. MODULE_ALIAS("platform:ax88796-pp");
  320. static struct platform_driver axdrv = {
  321. .driver = {
  322. .name = "ax88796-pp",
  323. },
  324. .probe = parport_ax88796_probe,
  325. .remove = parport_ax88796_remove,
  326. .suspend = parport_ax88796_suspend,
  327. .resume = parport_ax88796_resume,
  328. };
  329. module_platform_driver(axdrv);
  330. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  331. MODULE_DESCRIPTION("AX88796 Parport parallel port driver");
  332. MODULE_LICENSE("GPL");