pata_rb500_cf.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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_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_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. .tf_load = ata_tf_load,
  98. .tf_read = ata_tf_read,
  99. .exec_command = rb500_pata_exec_command,
  100. .check_status = ata_check_status,
  101. .dev_select = ata_std_dev_select,
  102. .data_xfer = rb500_pata_data_xfer,
  103. .qc_prep = ata_qc_prep,
  104. .qc_issue = ata_qc_issue_prot,
  105. .freeze = rb500_pata_freeze,
  106. .thaw = rb500_pata_thaw,
  107. .error_handler = ata_bmdma_error_handler,
  108. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  109. .irq_clear = ata_noop_irq_clear,
  110. .irq_on = ata_irq_on,
  111. };
  112. /* ------------------------------------------------------------------------ */
  113. static struct scsi_host_template rb500_pata_sht = {
  114. ATA_PIO_SHT(DRV_NAME),
  115. };
  116. /* ------------------------------------------------------------------------ */
  117. static void rb500_pata_setup_ports(struct ata_host *ah)
  118. {
  119. struct rb500_cf_info *info = ah->private_data;
  120. struct ata_port *ap;
  121. ap = ah->ports[0];
  122. ap->ops = &rb500_pata_port_ops;
  123. ap->pio_mask = 0x1f; /* PIO4 */
  124. ap->flags = ATA_FLAG_NO_LEGACY | ATA_FLAG_MMIO;
  125. ap->ioaddr.cmd_addr = info->iobase + RB500_CF_REG_CMD;
  126. ap->ioaddr.ctl_addr = info->iobase + RB500_CF_REG_CTRL;
  127. ap->ioaddr.altstatus_addr = info->iobase + RB500_CF_REG_CTRL;
  128. ata_std_ports(&ap->ioaddr);
  129. ap->ioaddr.data_addr = info->iobase + RB500_CF_REG_DATA;
  130. }
  131. static __devinit int rb500_pata_driver_probe(struct platform_device *pdev)
  132. {
  133. unsigned int irq;
  134. int gpio;
  135. struct resource *res;
  136. struct ata_host *ah;
  137. struct rb500_cf_info *info;
  138. int ret;
  139. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  140. if (!res) {
  141. dev_err(&pdev->dev, "no IOMEM resource found\n");
  142. return -EINVAL;
  143. }
  144. irq = platform_get_irq(pdev, 0);
  145. if (irq <= 0) {
  146. dev_err(&pdev->dev, "no IRQ resource found\n");
  147. return -ENOENT;
  148. }
  149. gpio = irq_to_gpio(irq);
  150. if (gpio < 0) {
  151. dev_err(&pdev->dev, "no GPIO found for irq%d\n", irq);
  152. return -ENOENT;
  153. }
  154. ret = gpio_request(gpio, DRV_NAME);
  155. if (ret) {
  156. dev_err(&pdev->dev, "GPIO request failed\n");
  157. return ret;
  158. }
  159. /* allocate host */
  160. ah = ata_host_alloc(&pdev->dev, RB500_CF_MAXPORTS);
  161. if (!ah)
  162. return -ENOMEM;
  163. platform_set_drvdata(pdev, ah);
  164. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  165. if (!info)
  166. return -ENOMEM;
  167. ah->private_data = info;
  168. info->gpio_line = gpio;
  169. info->irq = irq;
  170. info->iobase = devm_ioremap_nocache(&pdev->dev, res->start,
  171. res->end - res->start + 1);
  172. if (!info->iobase)
  173. return -ENOMEM;
  174. ret = gpio_direction_input(gpio);
  175. if (ret) {
  176. dev_err(&pdev->dev, "unable to set GPIO direction, err=%d\n",
  177. ret);
  178. goto err_free_gpio;
  179. }
  180. rb500_pata_setup_ports(ah);
  181. ret = ata_host_activate(ah, irq, rb500_pata_irq_handler,
  182. IRQF_TRIGGER_LOW, &rb500_pata_sht);
  183. if (ret)
  184. goto err_free_gpio;
  185. return 0;
  186. err_free_gpio:
  187. gpio_free(gpio);
  188. return ret;
  189. }
  190. static __devexit int rb500_pata_driver_remove(struct platform_device *pdev)
  191. {
  192. struct ata_host *ah = platform_get_drvdata(pdev);
  193. struct rb500_cf_info *info = ah->private_data;
  194. ata_host_detach(ah);
  195. gpio_free(info->gpio_line);
  196. return 0;
  197. }
  198. static struct platform_driver rb500_pata_platform_driver = {
  199. .probe = rb500_pata_driver_probe,
  200. .remove = __devexit_p(rb500_pata_driver_remove),
  201. .driver = {
  202. .name = DRV_NAME,
  203. .owner = THIS_MODULE,
  204. },
  205. };
  206. /* ------------------------------------------------------------------------ */
  207. #define DRV_INFO DRV_DESC " version " DRV_VERSION
  208. static int __init rb500_pata_module_init(void)
  209. {
  210. printk(KERN_INFO DRV_INFO "\n");
  211. return platform_driver_register(&rb500_pata_platform_driver);
  212. }
  213. static void __exit rb500_pata_module_exit(void)
  214. {
  215. platform_driver_unregister(&rb500_pata_platform_driver);
  216. }
  217. MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
  218. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  219. MODULE_DESCRIPTION(DRV_DESC);
  220. MODULE_VERSION(DRV_VERSION);
  221. MODULE_LICENSE("GPL");
  222. module_init(rb500_pata_module_init);
  223. module_exit(rb500_pata_module_exit);