pata_winbond.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * pata_winbond.c - Winbond VLB ATA controllers
  3. * (C) 2006 Red Hat <alan@redhat.com>
  4. *
  5. * Support for the Winbond 83759A when operating in advanced mode.
  6. * Multichip mode is not currently supported.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/delay.h>
  13. #include <scsi/scsi_host.h>
  14. #include <linux/libata.h>
  15. #include <linux/platform_device.h>
  16. #define DRV_NAME "pata_winbond"
  17. #define DRV_VERSION "0.0.2"
  18. #define NR_HOST 4 /* Two winbond controllers, two channels each */
  19. struct winbond_data {
  20. unsigned long config;
  21. struct platform_device *platform_dev;
  22. };
  23. static struct ata_host *winbond_host[NR_HOST];
  24. static struct winbond_data winbond_data[NR_HOST];
  25. static int nr_winbond_host;
  26. #ifdef MODULE
  27. static int probe_winbond = 1;
  28. #else
  29. static int probe_winbond;
  30. #endif
  31. static DEFINE_SPINLOCK(winbond_lock);
  32. static void winbond_writecfg(unsigned long port, u8 reg, u8 val)
  33. {
  34. unsigned long flags;
  35. spin_lock_irqsave(&winbond_lock, flags);
  36. outb(reg, port + 0x01);
  37. outb(val, port + 0x02);
  38. spin_unlock_irqrestore(&winbond_lock, flags);
  39. }
  40. static u8 winbond_readcfg(unsigned long port, u8 reg)
  41. {
  42. u8 val;
  43. unsigned long flags;
  44. spin_lock_irqsave(&winbond_lock, flags);
  45. outb(reg, port + 0x01);
  46. val = inb(port + 0x02);
  47. spin_unlock_irqrestore(&winbond_lock, flags);
  48. return val;
  49. }
  50. static void winbond_set_piomode(struct ata_port *ap, struct ata_device *adev)
  51. {
  52. struct ata_timing t;
  53. struct winbond_data *winbond = ap->host->private_data;
  54. int active, recovery;
  55. u8 reg;
  56. int timing = 0x88 + (ap->port_no * 4) + (adev->devno * 2);
  57. reg = winbond_readcfg(winbond->config, 0x81);
  58. /* Get the timing data in cycles */
  59. if (reg & 0x40) /* Fast VLB bus, assume 50MHz */
  60. ata_timing_compute(adev, adev->pio_mode, &t, 20000, 1000);
  61. else
  62. ata_timing_compute(adev, adev->pio_mode, &t, 30303, 1000);
  63. active = (FIT(t.active, 3, 17) - 1) & 0x0F;
  64. recovery = (FIT(t.recover, 1, 15) + 1) & 0x0F;
  65. timing = (active << 4) | recovery;
  66. winbond_writecfg(winbond->config, timing, reg);
  67. /* Load the setup timing */
  68. reg = 0x35;
  69. if (adev->class != ATA_DEV_ATA)
  70. reg |= 0x08; /* FIFO off */
  71. if (!ata_pio_need_iordy(adev))
  72. reg |= 0x02; /* IORDY off */
  73. reg |= (FIT(t.setup, 0, 3) << 6);
  74. winbond_writecfg(winbond->config, timing + 1, reg);
  75. }
  76. static void winbond_data_xfer(struct ata_device *adev, unsigned char *buf, unsigned int buflen, int write_data)
  77. {
  78. struct ata_port *ap = adev->ap;
  79. int slop = buflen & 3;
  80. if (ata_id_has_dword_io(adev->id)) {
  81. if (write_data)
  82. iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
  83. else
  84. ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
  85. if (unlikely(slop)) {
  86. u32 pad;
  87. if (write_data) {
  88. memcpy(&pad, buf + buflen - slop, slop);
  89. pad = le32_to_cpu(pad);
  90. iowrite32(pad, ap->ioaddr.data_addr);
  91. } else {
  92. pad = ioread32(ap->ioaddr.data_addr);
  93. pad = cpu_to_le16(pad);
  94. memcpy(buf + buflen - slop, &pad, slop);
  95. }
  96. }
  97. } else
  98. ata_data_xfer(adev, buf, buflen, write_data);
  99. }
  100. static struct scsi_host_template winbond_sht = {
  101. .module = THIS_MODULE,
  102. .name = DRV_NAME,
  103. .ioctl = ata_scsi_ioctl,
  104. .queuecommand = ata_scsi_queuecmd,
  105. .can_queue = ATA_DEF_QUEUE,
  106. .this_id = ATA_SHT_THIS_ID,
  107. .sg_tablesize = LIBATA_MAX_PRD,
  108. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  109. .emulated = ATA_SHT_EMULATED,
  110. .use_clustering = ATA_SHT_USE_CLUSTERING,
  111. .proc_name = DRV_NAME,
  112. .dma_boundary = ATA_DMA_BOUNDARY,
  113. .slave_configure = ata_scsi_slave_config,
  114. .slave_destroy = ata_scsi_slave_destroy,
  115. .bios_param = ata_std_bios_param,
  116. };
  117. static struct ata_port_operations winbond_port_ops = {
  118. .port_disable = ata_port_disable,
  119. .set_piomode = winbond_set_piomode,
  120. .tf_load = ata_tf_load,
  121. .tf_read = ata_tf_read,
  122. .check_status = ata_check_status,
  123. .exec_command = ata_exec_command,
  124. .dev_select = ata_std_dev_select,
  125. .freeze = ata_bmdma_freeze,
  126. .thaw = ata_bmdma_thaw,
  127. .error_handler = ata_bmdma_error_handler,
  128. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  129. .cable_detect = ata_cable_40wire,
  130. .qc_prep = ata_qc_prep,
  131. .qc_issue = ata_qc_issue_prot,
  132. .data_xfer = winbond_data_xfer,
  133. .irq_clear = ata_bmdma_irq_clear,
  134. .irq_on = ata_irq_on,
  135. .irq_ack = ata_irq_ack,
  136. .port_start = ata_port_start,
  137. };
  138. /**
  139. * winbond_init_one - attach a winbond interface
  140. * @type: Type to display
  141. * @io: I/O port start
  142. * @irq: interrupt line
  143. * @fast: True if on a > 33Mhz VLB
  144. *
  145. * Register a VLB bus IDE interface. Such interfaces are PIO and we
  146. * assume do not support IRQ sharing.
  147. */
  148. static __init int winbond_init_one(unsigned long port)
  149. {
  150. struct platform_device *pdev;
  151. u8 reg;
  152. int i, rc;
  153. reg = winbond_readcfg(port, 0x81);
  154. reg |= 0x80; /* jumpered mode off */
  155. winbond_writecfg(port, 0x81, reg);
  156. reg = winbond_readcfg(port, 0x83);
  157. reg |= 0xF0; /* local control */
  158. winbond_writecfg(port, 0x83, reg);
  159. reg = winbond_readcfg(port, 0x85);
  160. reg |= 0xF0; /* programmable timing */
  161. winbond_writecfg(port, 0x85, reg);
  162. reg = winbond_readcfg(port, 0x81);
  163. if (!(reg & 0x03)) /* Disabled */
  164. return 0;
  165. for (i = 0; i < 2 ; i ++) {
  166. unsigned long cmd_port = 0x1F0 - (0x80 * i);
  167. struct ata_host *host;
  168. struct ata_port *ap;
  169. void __iomem *cmd_addr, *ctl_addr;
  170. if (!(reg & (1 << i)))
  171. continue;
  172. pdev = platform_device_register_simple(DRV_NAME, nr_winbond_host, NULL, 0);
  173. if (IS_ERR(pdev))
  174. return PTR_ERR(pdev);
  175. rc = -ENOMEM;
  176. host = ata_host_alloc(&pdev->dev, 1);
  177. if (!host)
  178. goto err_unregister;
  179. rc = -ENOMEM;
  180. cmd_addr = devm_ioport_map(&pdev->dev, cmd_port, 8);
  181. ctl_addr = devm_ioport_map(&pdev->dev, cmd_port + 0x0206, 1);
  182. if (!cmd_addr || !ctl_addr)
  183. goto err_unregister;
  184. ap = host->ports[0];
  185. ap->ops = &winbond_port_ops;
  186. ap->pio_mask = 0x1F;
  187. ap->flags |= ATA_FLAG_SLAVE_POSS;
  188. ap->ioaddr.cmd_addr = cmd_addr;
  189. ap->ioaddr.altstatus_addr = ctl_addr;
  190. ap->ioaddr.ctl_addr = ctl_addr;
  191. ata_std_ports(&ap->ioaddr);
  192. /* hook in a private data structure per channel */
  193. host->private_data = &winbond_data[nr_winbond_host];
  194. winbond_data[nr_winbond_host].config = port;
  195. winbond_data[nr_winbond_host].platform_dev = pdev;
  196. /* activate */
  197. rc = ata_host_activate(host, 14 + i, ata_interrupt, 0,
  198. &winbond_sht);
  199. if (rc)
  200. goto err_unregister;
  201. winbond_host[nr_winbond_host++] = dev_get_drvdata(&pdev->dev);
  202. }
  203. return 0;
  204. err_unregister:
  205. platform_device_unregister(pdev);
  206. return rc;
  207. }
  208. /**
  209. * winbond_init - attach winbond interfaces
  210. *
  211. * Attach winbond IDE interfaces by scanning the ports it may occupy.
  212. */
  213. static __init int winbond_init(void)
  214. {
  215. static const unsigned long config[2] = { 0x130, 0x1B0 };
  216. int ct = 0;
  217. int i;
  218. if (probe_winbond == 0)
  219. return -ENODEV;
  220. /*
  221. * Check both base addresses
  222. */
  223. for (i = 0; i < 2; i++) {
  224. if (probe_winbond & (1<<i)) {
  225. int ret = 0;
  226. unsigned long port = config[i];
  227. if (request_region(port, 2, "pata_winbond")) {
  228. ret = winbond_init_one(port);
  229. if(ret <= 0)
  230. release_region(port, 2);
  231. else ct+= ret;
  232. }
  233. }
  234. }
  235. if (ct != 0)
  236. return 0;
  237. return -ENODEV;
  238. }
  239. static __exit void winbond_exit(void)
  240. {
  241. int i;
  242. for (i = 0; i < nr_winbond_host; i++) {
  243. ata_host_detach(winbond_host[i]);
  244. release_region(winbond_data[i].config, 2);
  245. platform_device_unregister(winbond_data[i].platform_dev);
  246. }
  247. }
  248. MODULE_AUTHOR("Alan Cox");
  249. MODULE_DESCRIPTION("low-level driver for Winbond VL ATA");
  250. MODULE_LICENSE("GPL");
  251. MODULE_VERSION(DRV_VERSION);
  252. module_init(winbond_init);
  253. module_exit(winbond_exit);
  254. module_param(probe_winbond, int, 0);