pata_rb500_cf.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * A low-level PATA driver to handle a Compact Flash connected on the
  3. * Mikrotik's RouterBoard 532 board.
  4. *
  5. * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
  6. * Copyright (C) 2008 Florian Fainelli <florian@openwrt.org>
  7. *
  8. * This file was based on: drivers/ata/pata_ixp4xx_cf.c
  9. * Copyright (C) 2006-07 Tower Technologies
  10. * Author: Alessandro Zummo <a.zummo@towertech.it>
  11. *
  12. * Also was based on the driver for Linux 2.4.xx published by Mikrotik for
  13. * their RouterBoard 1xx and 5xx series devices. The original Mikrotik code
  14. * seems not to have a license.
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License version 2 as
  18. * published by the Free Software Foundation.
  19. *
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/io.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/irq.h>
  27. #include <linux/libata.h>
  28. #include <scsi/scsi_host.h>
  29. #include <asm/gpio.h>
  30. #define DRV_NAME "pata-rb500-cf"
  31. #define DRV_VERSION "0.1.0"
  32. #define DRV_DESC "PATA driver for RouterBOARD 532 Compact Flash"
  33. #define RB500_CF_MAXPORTS 1
  34. #define RB500_CF_IO_DELAY 400
  35. #define RB500_CF_REG_CMD 0x0800
  36. #define RB500_CF_REG_CTRL 0x080E
  37. #define RB500_CF_REG_DATA 0x0C00
  38. struct rb500_cf_info {
  39. void __iomem *iobase;
  40. unsigned int gpio_line;
  41. int frozen;
  42. unsigned int irq;
  43. };
  44. /* ------------------------------------------------------------------------ */
  45. static inline void rb500_pata_finish_io(struct ata_port *ap)
  46. {
  47. struct ata_host *ah = ap->host;
  48. struct rb500_cf_info *info = ah->private_data;
  49. ata_sff_altstatus(ap);
  50. ndelay(RB500_CF_IO_DELAY);
  51. set_irq_type(info->irq, IRQ_TYPE_LEVEL_HIGH);
  52. }
  53. static void rb500_pata_exec_command(struct ata_port *ap,
  54. const struct ata_taskfile *tf)
  55. {
  56. writeb(tf->command, ap->ioaddr.command_addr);
  57. rb500_pata_finish_io(ap);
  58. }
  59. static void rb500_pata_data_xfer(struct ata_device *adev, unsigned char *buf,
  60. unsigned int buflen, int write_data)
  61. {
  62. struct ata_port *ap = adev->link->ap;
  63. void __iomem *ioaddr = ap->ioaddr.data_addr;
  64. if (write_data) {
  65. for (; buflen > 0; buflen--, buf++)
  66. writeb(*buf, ioaddr);
  67. } else {
  68. for (; buflen > 0; buflen--, buf++)
  69. *buf = readb(ioaddr);
  70. }
  71. rb500_pata_finish_io(adev->link->ap);
  72. }
  73. static void rb500_pata_freeze(struct ata_port *ap)
  74. {
  75. struct rb500_cf_info *info = ap->host->private_data;
  76. info->frozen = 1;
  77. }
  78. static void rb500_pata_thaw(struct ata_port *ap)
  79. {
  80. struct rb500_cf_info *info = ap->host->private_data;
  81. info->frozen = 0;
  82. }
  83. static irqreturn_t rb500_pata_irq_handler(int irq, void *dev_instance)
  84. {
  85. struct ata_host *ah = dev_instance;
  86. struct rb500_cf_info *info = ah->private_data;
  87. if (gpio_get_value(info->gpio_line)) {
  88. set_irq_type(info->irq, IRQ_TYPE_LEVEL_LOW);
  89. if (!info->frozen)
  90. ata_sff_interrupt(info->irq, dev_instance);
  91. } else {
  92. set_irq_type(info->irq, IRQ_TYPE_LEVEL_HIGH);
  93. }
  94. return IRQ_HANDLED;
  95. }
  96. static struct ata_port_operations rb500_pata_port_ops = {
  97. .inherits = &ata_sff_port_ops,
  98. .sff_exec_command = rb500_pata_exec_command,
  99. .sff_data_xfer = rb500_pata_data_xfer,
  100. .freeze = rb500_pata_freeze,
  101. .thaw = rb500_pata_thaw,
  102. };
  103. /* ------------------------------------------------------------------------ */
  104. static struct scsi_host_template rb500_pata_sht = {
  105. ATA_PIO_SHT(DRV_NAME),
  106. };
  107. /* ------------------------------------------------------------------------ */
  108. static void rb500_pata_setup_ports(struct ata_host *ah)
  109. {
  110. struct rb500_cf_info *info = ah->private_data;
  111. struct ata_port *ap;
  112. ap = ah->ports[0];
  113. ap->ops = &rb500_pata_port_ops;
  114. ap->pio_mask = 0x1f; /* PIO4 */
  115. ap->flags = ATA_FLAG_NO_LEGACY | ATA_FLAG_MMIO;
  116. ap->ioaddr.cmd_addr = info->iobase + RB500_CF_REG_CMD;
  117. ap->ioaddr.ctl_addr = info->iobase + RB500_CF_REG_CTRL;
  118. ap->ioaddr.altstatus_addr = info->iobase + RB500_CF_REG_CTRL;
  119. ata_sff_std_ports(&ap->ioaddr);
  120. ap->ioaddr.data_addr = info->iobase + RB500_CF_REG_DATA;
  121. }
  122. static __devinit int rb500_pata_driver_probe(struct platform_device *pdev)
  123. {
  124. unsigned int irq;
  125. int gpio;
  126. struct resource *res;
  127. struct ata_host *ah;
  128. struct rb500_cf_info *info;
  129. int ret;
  130. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  131. if (!res) {
  132. dev_err(&pdev->dev, "no IOMEM resource found\n");
  133. return -EINVAL;
  134. }
  135. irq = platform_get_irq(pdev, 0);
  136. if (irq <= 0) {
  137. dev_err(&pdev->dev, "no IRQ resource found\n");
  138. return -ENOENT;
  139. }
  140. gpio = irq_to_gpio(irq);
  141. if (gpio < 0) {
  142. dev_err(&pdev->dev, "no GPIO found for irq%d\n", irq);
  143. return -ENOENT;
  144. }
  145. ret = gpio_request(gpio, DRV_NAME);
  146. if (ret) {
  147. dev_err(&pdev->dev, "GPIO request failed\n");
  148. return ret;
  149. }
  150. /* allocate host */
  151. ah = ata_host_alloc(&pdev->dev, RB500_CF_MAXPORTS);
  152. if (!ah)
  153. return -ENOMEM;
  154. platform_set_drvdata(pdev, ah);
  155. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  156. if (!info)
  157. return -ENOMEM;
  158. ah->private_data = info;
  159. info->gpio_line = gpio;
  160. info->irq = irq;
  161. info->iobase = devm_ioremap_nocache(&pdev->dev, res->start,
  162. res->end - res->start + 1);
  163. if (!info->iobase)
  164. return -ENOMEM;
  165. ret = gpio_direction_input(gpio);
  166. if (ret) {
  167. dev_err(&pdev->dev, "unable to set GPIO direction, err=%d\n",
  168. ret);
  169. goto err_free_gpio;
  170. }
  171. rb500_pata_setup_ports(ah);
  172. ret = ata_host_activate(ah, irq, rb500_pata_irq_handler,
  173. IRQF_TRIGGER_LOW, &rb500_pata_sht);
  174. if (ret)
  175. goto err_free_gpio;
  176. return 0;
  177. err_free_gpio:
  178. gpio_free(gpio);
  179. return ret;
  180. }
  181. static __devexit int rb500_pata_driver_remove(struct platform_device *pdev)
  182. {
  183. struct ata_host *ah = platform_get_drvdata(pdev);
  184. struct rb500_cf_info *info = ah->private_data;
  185. ata_host_detach(ah);
  186. gpio_free(info->gpio_line);
  187. return 0;
  188. }
  189. /* work with hotplug and coldplug */
  190. MODULE_ALIAS("platform:" DRV_NAME);
  191. static struct platform_driver rb500_pata_platform_driver = {
  192. .probe = rb500_pata_driver_probe,
  193. .remove = __devexit_p(rb500_pata_driver_remove),
  194. .driver = {
  195. .name = DRV_NAME,
  196. .owner = THIS_MODULE,
  197. },
  198. };
  199. /* ------------------------------------------------------------------------ */
  200. #define DRV_INFO DRV_DESC " version " DRV_VERSION
  201. static int __init rb500_pata_module_init(void)
  202. {
  203. printk(KERN_INFO DRV_INFO "\n");
  204. return platform_driver_register(&rb500_pata_platform_driver);
  205. }
  206. static void __exit rb500_pata_module_exit(void)
  207. {
  208. platform_driver_unregister(&rb500_pata_platform_driver);
  209. }
  210. MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
  211. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  212. MODULE_DESCRIPTION(DRV_DESC);
  213. MODULE_VERSION(DRV_VERSION);
  214. MODULE_LICENSE("GPL");
  215. module_init(rb500_pata_module_init);
  216. module_exit(rb500_pata_module_exit);