pata_rb153_cf.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * A low-level PATA driver to handle a Compact Flash connected on the
  3. * Mikrotik's RouterBoard 153 board.
  4. *
  5. * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
  6. *
  7. * This file was based on: drivers/ata/pata_ixp4xx_cf.c
  8. * Copyright (C) 2006-07 Tower Technologies
  9. * Author: Alessandro Zummo <a.zummo@towertech.it>
  10. *
  11. * Also was based on the driver for Linux 2.4.xx published by Mikrotik for
  12. * their RouterBoard 1xx and 5xx series devices. The original Mikrotik code
  13. * seems not to have a license.
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License version 2 as published
  17. * by the Free Software Foundation.
  18. *
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/io.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/gpio.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/libata.h>
  28. #include <scsi/scsi_host.h>
  29. #define DRV_NAME "pata-rb153-cf"
  30. #define DRV_VERSION "0.5.0"
  31. #define DRV_DESC "PATA driver for RouterBOARD 153 Compact Flash"
  32. #define RB153_CF_MAXPORTS 1
  33. #define RB153_CF_IO_DELAY 100
  34. #define RB153_CF_REG_CMD 0x0800
  35. #define RB153_CF_REG_CTRL 0x080E
  36. #define RB153_CF_REG_DATA 0x0C00
  37. struct rb153_cf_info {
  38. void __iomem *iobase;
  39. unsigned int gpio_line;
  40. int frozen;
  41. unsigned int irq;
  42. };
  43. static inline void rb153_pata_finish_io(struct ata_port *ap)
  44. {
  45. struct rb153_cf_info *info = ap->host->private_data;
  46. /* FIXME: Keep previous delay. If this is merely a fence then
  47. * ata_sff_sync might be sufficient. */
  48. ata_sff_dma_pause(ap);
  49. ndelay(RB153_CF_IO_DELAY);
  50. irq_set_irq_type(info->irq, IRQ_TYPE_LEVEL_HIGH);
  51. }
  52. static void rb153_pata_exec_command(struct ata_port *ap,
  53. const struct ata_taskfile *tf)
  54. {
  55. writeb(tf->command, ap->ioaddr.command_addr);
  56. rb153_pata_finish_io(ap);
  57. }
  58. static unsigned int rb153_pata_data_xfer(struct ata_device *adev,
  59. unsigned char *buf,
  60. unsigned int buflen,
  61. int write_data)
  62. {
  63. void __iomem *ioaddr = adev->link->ap->ioaddr.data_addr;
  64. unsigned int t;
  65. t = buflen;
  66. if (write_data) {
  67. for (; t > 0; t--, buf++)
  68. writeb(*buf, ioaddr);
  69. } else {
  70. for (; t > 0; t--, buf++)
  71. *buf = readb(ioaddr);
  72. }
  73. rb153_pata_finish_io(adev->link->ap);
  74. return buflen;
  75. }
  76. static void rb153_pata_freeze(struct ata_port *ap)
  77. {
  78. struct rb153_cf_info *info = ap->host->private_data;
  79. info->frozen = 1;
  80. }
  81. static void rb153_pata_thaw(struct ata_port *ap)
  82. {
  83. struct rb153_cf_info *info = ap->host->private_data;
  84. info->frozen = 0;
  85. }
  86. static irqreturn_t rb153_pata_irq_handler(int irq, void *dev_instance)
  87. {
  88. struct ata_host *ah = dev_instance;
  89. struct rb153_cf_info *info = ah->private_data;
  90. if (gpio_get_value(info->gpio_line)) {
  91. irq_set_irq_type(info->irq, IRQ_TYPE_LEVEL_LOW);
  92. if (!info->frozen)
  93. ata_sff_interrupt(irq, dev_instance);
  94. } else {
  95. irq_set_irq_type(info->irq, IRQ_TYPE_LEVEL_HIGH);
  96. }
  97. return IRQ_HANDLED;
  98. }
  99. static struct ata_port_operations rb153_pata_port_ops = {
  100. .inherits = &ata_sff_port_ops,
  101. .sff_exec_command = rb153_pata_exec_command,
  102. .sff_data_xfer = rb153_pata_data_xfer,
  103. .freeze = rb153_pata_freeze,
  104. .thaw = rb153_pata_thaw,
  105. };
  106. static struct scsi_host_template rb153_pata_sht = {
  107. ATA_PIO_SHT(DRV_NAME),
  108. };
  109. static void rb153_pata_setup_port(struct ata_host *ah)
  110. {
  111. struct rb153_cf_info *info = ah->private_data;
  112. struct ata_port *ap;
  113. ap = ah->ports[0];
  114. ap->ops = &rb153_pata_port_ops;
  115. ap->pio_mask = 0x1f; /* PIO4 */
  116. ap->ioaddr.cmd_addr = info->iobase + RB153_CF_REG_CMD;
  117. ap->ioaddr.ctl_addr = info->iobase + RB153_CF_REG_CTRL;
  118. ap->ioaddr.altstatus_addr = info->iobase + RB153_CF_REG_CTRL;
  119. ata_sff_std_ports(&ap->ioaddr);
  120. ap->ioaddr.data_addr = info->iobase + RB153_CF_REG_DATA;
  121. }
  122. static int rb153_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 rb153_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. ah = ata_host_alloc(&pdev->dev, RB153_CF_MAXPORTS);
  151. if (!ah)
  152. return -ENOMEM;
  153. platform_set_drvdata(pdev, ah);
  154. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  155. if (!info)
  156. return -ENOMEM;
  157. ah->private_data = info;
  158. info->gpio_line = gpio;
  159. info->irq = irq;
  160. info->iobase = devm_ioremap_nocache(&pdev->dev, res->start,
  161. res->end - res->start + 1);
  162. if (!info->iobase)
  163. return -ENOMEM;
  164. ret = gpio_direction_input(gpio);
  165. if (ret) {
  166. dev_err(&pdev->dev, "unable to set GPIO direction, err=%d\n",
  167. ret);
  168. goto err_free_gpio;
  169. }
  170. rb153_pata_setup_port(ah);
  171. ret = ata_host_activate(ah, irq, rb153_pata_irq_handler,
  172. IRQF_TRIGGER_LOW, &rb153_pata_sht);
  173. if (ret)
  174. goto err_free_gpio;
  175. return 0;
  176. err_free_gpio:
  177. gpio_free(gpio);
  178. return ret;
  179. }
  180. static int rb153_pata_driver_remove(struct platform_device *pdev)
  181. {
  182. struct ata_host *ah = platform_get_drvdata(pdev);
  183. struct rb153_cf_info *info = ah->private_data;
  184. ata_host_detach(ah);
  185. gpio_free(info->gpio_line);
  186. return 0;
  187. }
  188. static struct platform_driver rb153_pata_platform_driver = {
  189. .probe = rb153_pata_driver_probe,
  190. .remove = rb153_pata_driver_remove,
  191. .driver = {
  192. .name = DRV_NAME,
  193. .owner = THIS_MODULE,
  194. },
  195. };
  196. /* ------------------------------------------------------------------------ */
  197. #define DRV_INFO DRV_DESC " version " DRV_VERSION
  198. static int __init rb153_pata_module_init(void)
  199. {
  200. printk(KERN_INFO DRV_INFO "\n");
  201. return platform_driver_register(&rb153_pata_platform_driver);
  202. }
  203. static void __exit rb153_pata_module_exit(void)
  204. {
  205. platform_driver_unregister(&rb153_pata_platform_driver);
  206. }
  207. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  208. MODULE_DESCRIPTION(DRV_DESC);
  209. MODULE_VERSION(DRV_VERSION);
  210. MODULE_LICENSE("GPL v2");
  211. module_init(rb153_pata_module_init);
  212. module_exit(rb153_pata_module_exit);