pata_winbond.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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.3"
  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 unsigned int winbond_data_xfer(struct ata_device *dev,
  77. unsigned char *buf, unsigned int buflen, int rw)
  78. {
  79. struct ata_port *ap = dev->link->ap;
  80. int slop = buflen & 3;
  81. if (ata_id_has_dword_io(dev->id)) {
  82. if (rw == READ)
  83. ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
  84. else
  85. iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
  86. if (unlikely(slop)) {
  87. u32 pad;
  88. if (rw == READ) {
  89. pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr));
  90. memcpy(buf + buflen - slop, &pad, slop);
  91. } else {
  92. memcpy(&pad, buf + buflen - slop, slop);
  93. iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
  94. }
  95. buflen += 4 - slop;
  96. }
  97. } else
  98. buflen = ata_data_xfer(dev, buf, buflen, rw);
  99. return buflen;
  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. .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. .port_start = ata_sff_port_start,
  136. };
  137. /**
  138. * winbond_init_one - attach a winbond interface
  139. * @type: Type to display
  140. * @io: I/O port start
  141. * @irq: interrupt line
  142. * @fast: True if on a > 33Mhz VLB
  143. *
  144. * Register a VLB bus IDE interface. Such interfaces are PIO and we
  145. * assume do not support IRQ sharing.
  146. */
  147. static __init int winbond_init_one(unsigned long port)
  148. {
  149. struct platform_device *pdev;
  150. u8 reg;
  151. int i, rc;
  152. reg = winbond_readcfg(port, 0x81);
  153. reg |= 0x80; /* jumpered mode off */
  154. winbond_writecfg(port, 0x81, reg);
  155. reg = winbond_readcfg(port, 0x83);
  156. reg |= 0xF0; /* local control */
  157. winbond_writecfg(port, 0x83, reg);
  158. reg = winbond_readcfg(port, 0x85);
  159. reg |= 0xF0; /* programmable timing */
  160. winbond_writecfg(port, 0x85, reg);
  161. reg = winbond_readcfg(port, 0x81);
  162. if (!(reg & 0x03)) /* Disabled */
  163. return -ENODEV;
  164. for (i = 0; i < 2 ; i ++) {
  165. unsigned long cmd_port = 0x1F0 - (0x80 * i);
  166. unsigned long ctl_port = cmd_port + 0x206;
  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. ap = host->ports[0];
  180. rc = -ENOMEM;
  181. cmd_addr = devm_ioport_map(&pdev->dev, cmd_port, 8);
  182. ctl_addr = devm_ioport_map(&pdev->dev, ctl_port, 1);
  183. if (!cmd_addr || !ctl_addr)
  184. goto err_unregister;
  185. ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", cmd_port, ctl_port);
  186. ap->ops = &winbond_port_ops;
  187. ap->pio_mask = 0x1F;
  188. ap->flags |= ATA_FLAG_SLAVE_POSS;
  189. ap->ioaddr.cmd_addr = cmd_addr;
  190. ap->ioaddr.altstatus_addr = ctl_addr;
  191. ap->ioaddr.ctl_addr = ctl_addr;
  192. ata_std_ports(&ap->ioaddr);
  193. /* hook in a private data structure per channel */
  194. host->private_data = &winbond_data[nr_winbond_host];
  195. winbond_data[nr_winbond_host].config = port;
  196. winbond_data[nr_winbond_host].platform_dev = pdev;
  197. /* activate */
  198. rc = ata_host_activate(host, 14 + i, ata_interrupt, 0,
  199. &winbond_sht);
  200. if (rc)
  201. goto err_unregister;
  202. winbond_host[nr_winbond_host++] = dev_get_drvdata(&pdev->dev);
  203. }
  204. return 0;
  205. err_unregister:
  206. platform_device_unregister(pdev);
  207. return rc;
  208. }
  209. /**
  210. * winbond_init - attach winbond interfaces
  211. *
  212. * Attach winbond IDE interfaces by scanning the ports it may occupy.
  213. */
  214. static __init int winbond_init(void)
  215. {
  216. static const unsigned long config[2] = { 0x130, 0x1B0 };
  217. int ct = 0;
  218. int i;
  219. if (probe_winbond == 0)
  220. return -ENODEV;
  221. /*
  222. * Check both base addresses
  223. */
  224. for (i = 0; i < 2; i++) {
  225. if (probe_winbond & (1<<i)) {
  226. int ret = 0;
  227. unsigned long port = config[i];
  228. if (request_region(port, 2, "pata_winbond")) {
  229. ret = winbond_init_one(port);
  230. if (ret <= 0)
  231. release_region(port, 2);
  232. else ct+= ret;
  233. }
  234. }
  235. }
  236. if (ct != 0)
  237. return 0;
  238. return -ENODEV;
  239. }
  240. static __exit void winbond_exit(void)
  241. {
  242. int i;
  243. for (i = 0; i < nr_winbond_host; i++) {
  244. ata_host_detach(winbond_host[i]);
  245. release_region(winbond_data[i].config, 2);
  246. platform_device_unregister(winbond_data[i].platform_dev);
  247. }
  248. }
  249. MODULE_AUTHOR("Alan Cox");
  250. MODULE_DESCRIPTION("low-level driver for Winbond VL ATA");
  251. MODULE_LICENSE("GPL");
  252. MODULE_VERSION(DRV_VERSION);
  253. module_init(winbond_init);
  254. module_exit(winbond_exit);
  255. module_param(probe_winbond, int, 0);