pata_winbond.c 7.5 KB

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