pata_winbond.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 = (clamp_val(t.active, 3, 17) - 1) & 0x0F;
  64. recovery = (clamp_val(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 |= (clamp_val(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. __le32 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_sff_data_xfer(dev, buf, buflen, rw);
  99. return buflen;
  100. }
  101. static struct scsi_host_template winbond_sht = {
  102. ATA_PIO_SHT(DRV_NAME),
  103. };
  104. static struct ata_port_operations winbond_port_ops = {
  105. .inherits = &ata_sff_port_ops,
  106. .sff_data_xfer = winbond_data_xfer,
  107. .cable_detect = ata_cable_40wire,
  108. .set_piomode = winbond_set_piomode,
  109. };
  110. /**
  111. * winbond_init_one - attach a winbond interface
  112. * @type: Type to display
  113. * @io: I/O port start
  114. * @irq: interrupt line
  115. * @fast: True if on a > 33Mhz VLB
  116. *
  117. * Register a VLB bus IDE interface. Such interfaces are PIO and we
  118. * assume do not support IRQ sharing.
  119. */
  120. static __init int winbond_init_one(unsigned long port)
  121. {
  122. struct platform_device *pdev;
  123. u8 reg;
  124. int i, rc;
  125. reg = winbond_readcfg(port, 0x81);
  126. reg |= 0x80; /* jumpered mode off */
  127. winbond_writecfg(port, 0x81, reg);
  128. reg = winbond_readcfg(port, 0x83);
  129. reg |= 0xF0; /* local control */
  130. winbond_writecfg(port, 0x83, reg);
  131. reg = winbond_readcfg(port, 0x85);
  132. reg |= 0xF0; /* programmable timing */
  133. winbond_writecfg(port, 0x85, reg);
  134. reg = winbond_readcfg(port, 0x81);
  135. if (!(reg & 0x03)) /* Disabled */
  136. return -ENODEV;
  137. for (i = 0; i < 2 ; i ++) {
  138. unsigned long cmd_port = 0x1F0 - (0x80 * i);
  139. unsigned long ctl_port = cmd_port + 0x206;
  140. struct ata_host *host;
  141. struct ata_port *ap;
  142. void __iomem *cmd_addr, *ctl_addr;
  143. if (!(reg & (1 << i)))
  144. continue;
  145. pdev = platform_device_register_simple(DRV_NAME, nr_winbond_host, NULL, 0);
  146. if (IS_ERR(pdev))
  147. return PTR_ERR(pdev);
  148. rc = -ENOMEM;
  149. host = ata_host_alloc(&pdev->dev, 1);
  150. if (!host)
  151. goto err_unregister;
  152. ap = host->ports[0];
  153. rc = -ENOMEM;
  154. cmd_addr = devm_ioport_map(&pdev->dev, cmd_port, 8);
  155. ctl_addr = devm_ioport_map(&pdev->dev, ctl_port, 1);
  156. if (!cmd_addr || !ctl_addr)
  157. goto err_unregister;
  158. ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", cmd_port, ctl_port);
  159. ap->ops = &winbond_port_ops;
  160. ap->pio_mask = 0x1F;
  161. ap->flags |= ATA_FLAG_SLAVE_POSS;
  162. ap->ioaddr.cmd_addr = cmd_addr;
  163. ap->ioaddr.altstatus_addr = ctl_addr;
  164. ap->ioaddr.ctl_addr = ctl_addr;
  165. ata_sff_std_ports(&ap->ioaddr);
  166. /* hook in a private data structure per channel */
  167. host->private_data = &winbond_data[nr_winbond_host];
  168. winbond_data[nr_winbond_host].config = port;
  169. winbond_data[nr_winbond_host].platform_dev = pdev;
  170. /* activate */
  171. rc = ata_host_activate(host, 14 + i, ata_sff_interrupt, 0,
  172. &winbond_sht);
  173. if (rc)
  174. goto err_unregister;
  175. winbond_host[nr_winbond_host++] = dev_get_drvdata(&pdev->dev);
  176. }
  177. return 0;
  178. err_unregister:
  179. platform_device_unregister(pdev);
  180. return rc;
  181. }
  182. /**
  183. * winbond_init - attach winbond interfaces
  184. *
  185. * Attach winbond IDE interfaces by scanning the ports it may occupy.
  186. */
  187. static __init int winbond_init(void)
  188. {
  189. static const unsigned long config[2] = { 0x130, 0x1B0 };
  190. int ct = 0;
  191. int i;
  192. if (probe_winbond == 0)
  193. return -ENODEV;
  194. /*
  195. * Check both base addresses
  196. */
  197. for (i = 0; i < 2; i++) {
  198. if (probe_winbond & (1<<i)) {
  199. int ret = 0;
  200. unsigned long port = config[i];
  201. if (request_region(port, 2, "pata_winbond")) {
  202. ret = winbond_init_one(port);
  203. if (ret <= 0)
  204. release_region(port, 2);
  205. else ct+= ret;
  206. }
  207. }
  208. }
  209. if (ct != 0)
  210. return 0;
  211. return -ENODEV;
  212. }
  213. static __exit void winbond_exit(void)
  214. {
  215. int i;
  216. for (i = 0; i < nr_winbond_host; i++) {
  217. ata_host_detach(winbond_host[i]);
  218. release_region(winbond_data[i].config, 2);
  219. platform_device_unregister(winbond_data[i].platform_dev);
  220. }
  221. }
  222. MODULE_AUTHOR("Alan Cox");
  223. MODULE_DESCRIPTION("low-level driver for Winbond VL ATA");
  224. MODULE_LICENSE("GPL");
  225. MODULE_VERSION(DRV_VERSION);
  226. module_init(winbond_init);
  227. module_exit(winbond_exit);
  228. module_param(probe_winbond, int, 0);