pata_rb500_cf.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. .module = THIS_MODULE,
  115. .name = DRV_NAME,
  116. .ioctl = ata_scsi_ioctl,
  117. .queuecommand = ata_scsi_queuecmd,
  118. .slave_configure = ata_scsi_slave_config,
  119. .slave_destroy = ata_scsi_slave_destroy,
  120. .bios_param = ata_std_bios_param,
  121. .proc_name = DRV_NAME,
  122. .can_queue = ATA_DEF_QUEUE,
  123. .this_id = ATA_SHT_THIS_ID,
  124. .sg_tablesize = LIBATA_MAX_PRD,
  125. .dma_boundary = ATA_DMA_BOUNDARY,
  126. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  127. .emulated = ATA_SHT_EMULATED,
  128. .use_clustering = ATA_SHT_USE_CLUSTERING,
  129. };
  130. /* ------------------------------------------------------------------------ */
  131. static void rb500_pata_setup_ports(struct ata_host *ah)
  132. {
  133. struct rb500_cf_info *info = ah->private_data;
  134. struct ata_port *ap;
  135. ap = ah->ports[0];
  136. ap->ops = &rb500_pata_port_ops;
  137. ap->pio_mask = 0x1f; /* PIO4 */
  138. ap->flags = ATA_FLAG_NO_LEGACY | ATA_FLAG_MMIO;
  139. ap->ioaddr.cmd_addr = info->iobase + RB500_CF_REG_CMD;
  140. ap->ioaddr.ctl_addr = info->iobase + RB500_CF_REG_CTRL;
  141. ap->ioaddr.altstatus_addr = info->iobase + RB500_CF_REG_CTRL;
  142. ata_std_ports(&ap->ioaddr);
  143. ap->ioaddr.data_addr = info->iobase + RB500_CF_REG_DATA;
  144. }
  145. static __devinit int rb500_pata_driver_probe(struct platform_device *pdev)
  146. {
  147. unsigned int irq;
  148. int gpio;
  149. struct resource *res;
  150. struct ata_host *ah;
  151. struct rb500_cf_info *info;
  152. int ret;
  153. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  154. if (!res) {
  155. dev_err(&pdev->dev, "no IOMEM resource found\n");
  156. return -EINVAL;
  157. }
  158. irq = platform_get_irq(pdev, 0);
  159. if (irq <= 0) {
  160. dev_err(&pdev->dev, "no IRQ resource found\n");
  161. return -ENOENT;
  162. }
  163. gpio = irq_to_gpio(irq);
  164. if (gpio < 0) {
  165. dev_err(&pdev->dev, "no GPIO found for irq%d\n", irq);
  166. return -ENOENT;
  167. }
  168. ret = gpio_request(gpio, DRV_NAME);
  169. if (ret) {
  170. dev_err(&pdev->dev, "GPIO request failed\n");
  171. return ret;
  172. }
  173. /* allocate host */
  174. ah = ata_host_alloc(&pdev->dev, RB500_CF_MAXPORTS);
  175. if (!ah)
  176. return -ENOMEM;
  177. platform_set_drvdata(pdev, ah);
  178. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  179. if (!info)
  180. return -ENOMEM;
  181. ah->private_data = info;
  182. info->gpio_line = gpio;
  183. info->irq = irq;
  184. info->iobase = devm_ioremap_nocache(&pdev->dev, res->start,
  185. res->end - res->start + 1);
  186. if (!info->iobase)
  187. return -ENOMEM;
  188. ret = gpio_direction_input(gpio);
  189. if (ret) {
  190. dev_err(&pdev->dev, "unable to set GPIO direction, err=%d\n",
  191. ret);
  192. goto err_free_gpio;
  193. }
  194. rb500_pata_setup_ports(ah);
  195. ret = ata_host_activate(ah, irq, rb500_pata_irq_handler,
  196. IRQF_TRIGGER_LOW, &rb500_pata_sht);
  197. if (ret)
  198. goto err_free_gpio;
  199. return 0;
  200. err_free_gpio:
  201. gpio_free(gpio);
  202. return ret;
  203. }
  204. static __devexit int rb500_pata_driver_remove(struct platform_device *pdev)
  205. {
  206. struct ata_host *ah = platform_get_drvdata(pdev);
  207. struct rb500_cf_info *info = ah->private_data;
  208. ata_host_detach(ah);
  209. gpio_free(info->gpio_line);
  210. return 0;
  211. }
  212. static struct platform_driver rb500_pata_platform_driver = {
  213. .probe = rb500_pata_driver_probe,
  214. .remove = __devexit_p(rb500_pata_driver_remove),
  215. .driver = {
  216. .name = DRV_NAME,
  217. .owner = THIS_MODULE,
  218. },
  219. };
  220. /* ------------------------------------------------------------------------ */
  221. #define DRV_INFO DRV_DESC " version " DRV_VERSION
  222. static int __init rb500_pata_module_init(void)
  223. {
  224. printk(KERN_INFO DRV_INFO "\n");
  225. return platform_driver_register(&rb500_pata_platform_driver);
  226. }
  227. static void __exit rb500_pata_module_exit(void)
  228. {
  229. platform_driver_unregister(&rb500_pata_platform_driver);
  230. }
  231. MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
  232. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  233. MODULE_DESCRIPTION(DRV_DESC);
  234. MODULE_VERSION(DRV_VERSION);
  235. MODULE_LICENSE("GPL");
  236. module_init(rb500_pata_module_init);
  237. module_exit(rb500_pata_module_exit);