sata_sx4.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  1. /*
  2. * sata_sx4.c - Promise SATA
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2003-2004 Red Hat, Inc.
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; see the file COPYING. If not, write to
  23. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. *
  26. * libata documentation is available via 'make {ps|pdf}docs',
  27. * as Documentation/DocBook/libata.*
  28. *
  29. * Hardware documentation available under NDA.
  30. *
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/pci.h>
  35. #include <linux/init.h>
  36. #include <linux/blkdev.h>
  37. #include <linux/delay.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/sched.h>
  40. #include <linux/device.h>
  41. #include <scsi/scsi_host.h>
  42. #include <scsi/scsi_cmnd.h>
  43. #include <linux/libata.h>
  44. #include <asm/io.h>
  45. #include "sata_promise.h"
  46. #define DRV_NAME "sata_sx4"
  47. #define DRV_VERSION "0.8"
  48. enum {
  49. PDC_PRD_TBL = 0x44, /* Direct command DMA table addr */
  50. PDC_PKT_SUBMIT = 0x40, /* Command packet pointer addr */
  51. PDC_HDMA_PKT_SUBMIT = 0x100, /* Host DMA packet pointer addr */
  52. PDC_INT_SEQMASK = 0x40, /* Mask of asserted SEQ INTs */
  53. PDC_HDMA_CTLSTAT = 0x12C, /* Host DMA control / status */
  54. PDC_20621_SEQCTL = 0x400,
  55. PDC_20621_SEQMASK = 0x480,
  56. PDC_20621_GENERAL_CTL = 0x484,
  57. PDC_20621_PAGE_SIZE = (32 * 1024),
  58. /* chosen, not constant, values; we design our own DIMM mem map */
  59. PDC_20621_DIMM_WINDOW = 0x0C, /* page# for 32K DIMM window */
  60. PDC_20621_DIMM_BASE = 0x00200000,
  61. PDC_20621_DIMM_DATA = (64 * 1024),
  62. PDC_DIMM_DATA_STEP = (256 * 1024),
  63. PDC_DIMM_WINDOW_STEP = (8 * 1024),
  64. PDC_DIMM_HOST_PRD = (6 * 1024),
  65. PDC_DIMM_HOST_PKT = (128 * 0),
  66. PDC_DIMM_HPKT_PRD = (128 * 1),
  67. PDC_DIMM_ATA_PKT = (128 * 2),
  68. PDC_DIMM_APKT_PRD = (128 * 3),
  69. PDC_DIMM_HEADER_SZ = PDC_DIMM_APKT_PRD + 128,
  70. PDC_PAGE_WINDOW = 0x40,
  71. PDC_PAGE_DATA = PDC_PAGE_WINDOW +
  72. (PDC_20621_DIMM_DATA / PDC_20621_PAGE_SIZE),
  73. PDC_PAGE_SET = PDC_DIMM_DATA_STEP / PDC_20621_PAGE_SIZE,
  74. PDC_CHIP0_OFS = 0xC0000, /* offset of chip #0 */
  75. PDC_20621_ERR_MASK = (1<<19) | (1<<20) | (1<<21) | (1<<22) |
  76. (1<<23),
  77. board_20621 = 0, /* FastTrak S150 SX4 */
  78. PDC_RESET = (1 << 11), /* HDMA reset */
  79. PDC_MAX_HDMA = 32,
  80. PDC_HDMA_Q_MASK = (PDC_MAX_HDMA - 1),
  81. PDC_DIMM0_SPD_DEV_ADDRESS = 0x50,
  82. PDC_DIMM1_SPD_DEV_ADDRESS = 0x51,
  83. PDC_MAX_DIMM_MODULE = 0x02,
  84. PDC_I2C_CONTROL_OFFSET = 0x48,
  85. PDC_I2C_ADDR_DATA_OFFSET = 0x4C,
  86. PDC_DIMM0_CONTROL_OFFSET = 0x80,
  87. PDC_DIMM1_CONTROL_OFFSET = 0x84,
  88. PDC_SDRAM_CONTROL_OFFSET = 0x88,
  89. PDC_I2C_WRITE = 0x00000000,
  90. PDC_I2C_READ = 0x00000040,
  91. PDC_I2C_START = 0x00000080,
  92. PDC_I2C_MASK_INT = 0x00000020,
  93. PDC_I2C_COMPLETE = 0x00010000,
  94. PDC_I2C_NO_ACK = 0x00100000,
  95. PDC_DIMM_SPD_SUBADDRESS_START = 0x00,
  96. PDC_DIMM_SPD_SUBADDRESS_END = 0x7F,
  97. PDC_DIMM_SPD_ROW_NUM = 3,
  98. PDC_DIMM_SPD_COLUMN_NUM = 4,
  99. PDC_DIMM_SPD_MODULE_ROW = 5,
  100. PDC_DIMM_SPD_TYPE = 11,
  101. PDC_DIMM_SPD_FRESH_RATE = 12,
  102. PDC_DIMM_SPD_BANK_NUM = 17,
  103. PDC_DIMM_SPD_CAS_LATENCY = 18,
  104. PDC_DIMM_SPD_ATTRIBUTE = 21,
  105. PDC_DIMM_SPD_ROW_PRE_CHARGE = 27,
  106. PDC_DIMM_SPD_ROW_ACTIVE_DELAY = 28,
  107. PDC_DIMM_SPD_RAS_CAS_DELAY = 29,
  108. PDC_DIMM_SPD_ACTIVE_PRECHARGE = 30,
  109. PDC_DIMM_SPD_SYSTEM_FREQ = 126,
  110. PDC_CTL_STATUS = 0x08,
  111. PDC_DIMM_WINDOW_CTLR = 0x0C,
  112. PDC_TIME_CONTROL = 0x3C,
  113. PDC_TIME_PERIOD = 0x40,
  114. PDC_TIME_COUNTER = 0x44,
  115. PDC_GENERAL_CTLR = 0x484,
  116. PCI_PLL_INIT = 0x8A531824,
  117. PCI_X_TCOUNT = 0xEE1E5CFF
  118. };
  119. struct pdc_port_priv {
  120. u8 dimm_buf[(ATA_PRD_SZ * ATA_MAX_PRD) + 512];
  121. u8 *pkt;
  122. dma_addr_t pkt_dma;
  123. };
  124. struct pdc_host_priv {
  125. void __iomem *dimm_mmio;
  126. unsigned int doing_hdma;
  127. unsigned int hdma_prod;
  128. unsigned int hdma_cons;
  129. struct {
  130. struct ata_queued_cmd *qc;
  131. unsigned int seq;
  132. unsigned long pkt_ofs;
  133. } hdma[32];
  134. };
  135. static int pdc_sata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  136. static irqreturn_t pdc20621_interrupt (int irq, void *dev_instance, struct pt_regs *regs);
  137. static void pdc_eng_timeout(struct ata_port *ap);
  138. static void pdc_20621_phy_reset (struct ata_port *ap);
  139. static int pdc_port_start(struct ata_port *ap);
  140. static void pdc_port_stop(struct ata_port *ap);
  141. static void pdc20621_qc_prep(struct ata_queued_cmd *qc);
  142. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf);
  143. static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf);
  144. static void pdc20621_host_stop(struct ata_host_set *host_set);
  145. static unsigned int pdc20621_dimm_init(struct ata_probe_ent *pe);
  146. static int pdc20621_detect_dimm(struct ata_probe_ent *pe);
  147. static unsigned int pdc20621_i2c_read(struct ata_probe_ent *pe,
  148. u32 device, u32 subaddr, u32 *pdata);
  149. static int pdc20621_prog_dimm0(struct ata_probe_ent *pe);
  150. static unsigned int pdc20621_prog_dimm_global(struct ata_probe_ent *pe);
  151. #ifdef ATA_VERBOSE_DEBUG
  152. static void pdc20621_get_from_dimm(struct ata_probe_ent *pe,
  153. void *psource, u32 offset, u32 size);
  154. #endif
  155. static void pdc20621_put_to_dimm(struct ata_probe_ent *pe,
  156. void *psource, u32 offset, u32 size);
  157. static void pdc20621_irq_clear(struct ata_port *ap);
  158. static int pdc20621_qc_issue_prot(struct ata_queued_cmd *qc);
  159. static struct scsi_host_template pdc_sata_sht = {
  160. .module = THIS_MODULE,
  161. .name = DRV_NAME,
  162. .ioctl = ata_scsi_ioctl,
  163. .queuecommand = ata_scsi_queuecmd,
  164. .eh_strategy_handler = ata_scsi_error,
  165. .can_queue = ATA_DEF_QUEUE,
  166. .this_id = ATA_SHT_THIS_ID,
  167. .sg_tablesize = LIBATA_MAX_PRD,
  168. .max_sectors = ATA_MAX_SECTORS,
  169. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  170. .emulated = ATA_SHT_EMULATED,
  171. .use_clustering = ATA_SHT_USE_CLUSTERING,
  172. .proc_name = DRV_NAME,
  173. .dma_boundary = ATA_DMA_BOUNDARY,
  174. .slave_configure = ata_scsi_slave_config,
  175. .bios_param = ata_std_bios_param,
  176. };
  177. static const struct ata_port_operations pdc_20621_ops = {
  178. .port_disable = ata_port_disable,
  179. .tf_load = pdc_tf_load_mmio,
  180. .tf_read = ata_tf_read,
  181. .check_status = ata_check_status,
  182. .exec_command = pdc_exec_command_mmio,
  183. .dev_select = ata_std_dev_select,
  184. .phy_reset = pdc_20621_phy_reset,
  185. .qc_prep = pdc20621_qc_prep,
  186. .qc_issue = pdc20621_qc_issue_prot,
  187. .eng_timeout = pdc_eng_timeout,
  188. .irq_handler = pdc20621_interrupt,
  189. .irq_clear = pdc20621_irq_clear,
  190. .port_start = pdc_port_start,
  191. .port_stop = pdc_port_stop,
  192. .host_stop = pdc20621_host_stop,
  193. };
  194. static const struct ata_port_info pdc_port_info[] = {
  195. /* board_20621 */
  196. {
  197. .sht = &pdc_sata_sht,
  198. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  199. ATA_FLAG_SRST | ATA_FLAG_MMIO |
  200. ATA_FLAG_NO_ATAPI,
  201. .pio_mask = 0x1f, /* pio0-4 */
  202. .mwdma_mask = 0x07, /* mwdma0-2 */
  203. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  204. .port_ops = &pdc_20621_ops,
  205. },
  206. };
  207. static const struct pci_device_id pdc_sata_pci_tbl[] = {
  208. { PCI_VENDOR_ID_PROMISE, 0x6622, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  209. board_20621 },
  210. { } /* terminate list */
  211. };
  212. static struct pci_driver pdc_sata_pci_driver = {
  213. .name = DRV_NAME,
  214. .id_table = pdc_sata_pci_tbl,
  215. .probe = pdc_sata_init_one,
  216. .remove = ata_pci_remove_one,
  217. };
  218. static void pdc20621_host_stop(struct ata_host_set *host_set)
  219. {
  220. struct pci_dev *pdev = to_pci_dev(host_set->dev);
  221. struct pdc_host_priv *hpriv = host_set->private_data;
  222. void __iomem *dimm_mmio = hpriv->dimm_mmio;
  223. pci_iounmap(pdev, dimm_mmio);
  224. kfree(hpriv);
  225. pci_iounmap(pdev, host_set->mmio_base);
  226. }
  227. static int pdc_port_start(struct ata_port *ap)
  228. {
  229. struct device *dev = ap->host_set->dev;
  230. struct pdc_port_priv *pp;
  231. int rc;
  232. rc = ata_port_start(ap);
  233. if (rc)
  234. return rc;
  235. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  236. if (!pp) {
  237. rc = -ENOMEM;
  238. goto err_out;
  239. }
  240. memset(pp, 0, sizeof(*pp));
  241. pp->pkt = dma_alloc_coherent(dev, 128, &pp->pkt_dma, GFP_KERNEL);
  242. if (!pp->pkt) {
  243. rc = -ENOMEM;
  244. goto err_out_kfree;
  245. }
  246. ap->private_data = pp;
  247. return 0;
  248. err_out_kfree:
  249. kfree(pp);
  250. err_out:
  251. ata_port_stop(ap);
  252. return rc;
  253. }
  254. static void pdc_port_stop(struct ata_port *ap)
  255. {
  256. struct device *dev = ap->host_set->dev;
  257. struct pdc_port_priv *pp = ap->private_data;
  258. ap->private_data = NULL;
  259. dma_free_coherent(dev, 128, pp->pkt, pp->pkt_dma);
  260. kfree(pp);
  261. ata_port_stop(ap);
  262. }
  263. static void pdc_20621_phy_reset (struct ata_port *ap)
  264. {
  265. VPRINTK("ENTER\n");
  266. ap->cbl = ATA_CBL_SATA;
  267. ata_port_probe(ap);
  268. ata_bus_reset(ap);
  269. }
  270. static inline void pdc20621_ata_sg(struct ata_taskfile *tf, u8 *buf,
  271. unsigned int portno,
  272. unsigned int total_len)
  273. {
  274. u32 addr;
  275. unsigned int dw = PDC_DIMM_APKT_PRD >> 2;
  276. u32 *buf32 = (u32 *) buf;
  277. /* output ATA packet S/G table */
  278. addr = PDC_20621_DIMM_BASE + PDC_20621_DIMM_DATA +
  279. (PDC_DIMM_DATA_STEP * portno);
  280. VPRINTK("ATA sg addr 0x%x, %d\n", addr, addr);
  281. buf32[dw] = cpu_to_le32(addr);
  282. buf32[dw + 1] = cpu_to_le32(total_len | ATA_PRD_EOT);
  283. VPRINTK("ATA PSG @ %x == (0x%x, 0x%x)\n",
  284. PDC_20621_DIMM_BASE +
  285. (PDC_DIMM_WINDOW_STEP * portno) +
  286. PDC_DIMM_APKT_PRD,
  287. buf32[dw], buf32[dw + 1]);
  288. }
  289. static inline void pdc20621_host_sg(struct ata_taskfile *tf, u8 *buf,
  290. unsigned int portno,
  291. unsigned int total_len)
  292. {
  293. u32 addr;
  294. unsigned int dw = PDC_DIMM_HPKT_PRD >> 2;
  295. u32 *buf32 = (u32 *) buf;
  296. /* output Host DMA packet S/G table */
  297. addr = PDC_20621_DIMM_BASE + PDC_20621_DIMM_DATA +
  298. (PDC_DIMM_DATA_STEP * portno);
  299. buf32[dw] = cpu_to_le32(addr);
  300. buf32[dw + 1] = cpu_to_le32(total_len | ATA_PRD_EOT);
  301. VPRINTK("HOST PSG @ %x == (0x%x, 0x%x)\n",
  302. PDC_20621_DIMM_BASE +
  303. (PDC_DIMM_WINDOW_STEP * portno) +
  304. PDC_DIMM_HPKT_PRD,
  305. buf32[dw], buf32[dw + 1]);
  306. }
  307. static inline unsigned int pdc20621_ata_pkt(struct ata_taskfile *tf,
  308. unsigned int devno, u8 *buf,
  309. unsigned int portno)
  310. {
  311. unsigned int i, dw;
  312. u32 *buf32 = (u32 *) buf;
  313. u8 dev_reg;
  314. unsigned int dimm_sg = PDC_20621_DIMM_BASE +
  315. (PDC_DIMM_WINDOW_STEP * portno) +
  316. PDC_DIMM_APKT_PRD;
  317. VPRINTK("ENTER, dimm_sg == 0x%x, %d\n", dimm_sg, dimm_sg);
  318. i = PDC_DIMM_ATA_PKT;
  319. /*
  320. * Set up ATA packet
  321. */
  322. if ((tf->protocol == ATA_PROT_DMA) && (!(tf->flags & ATA_TFLAG_WRITE)))
  323. buf[i++] = PDC_PKT_READ;
  324. else if (tf->protocol == ATA_PROT_NODATA)
  325. buf[i++] = PDC_PKT_NODATA;
  326. else
  327. buf[i++] = 0;
  328. buf[i++] = 0; /* reserved */
  329. buf[i++] = portno + 1; /* seq. id */
  330. buf[i++] = 0xff; /* delay seq. id */
  331. /* dimm dma S/G, and next-pkt */
  332. dw = i >> 2;
  333. if (tf->protocol == ATA_PROT_NODATA)
  334. buf32[dw] = 0;
  335. else
  336. buf32[dw] = cpu_to_le32(dimm_sg);
  337. buf32[dw + 1] = 0;
  338. i += 8;
  339. if (devno == 0)
  340. dev_reg = ATA_DEVICE_OBS;
  341. else
  342. dev_reg = ATA_DEVICE_OBS | ATA_DEV1;
  343. /* select device */
  344. buf[i++] = (1 << 5) | PDC_PKT_CLEAR_BSY | ATA_REG_DEVICE;
  345. buf[i++] = dev_reg;
  346. /* device control register */
  347. buf[i++] = (1 << 5) | PDC_REG_DEVCTL;
  348. buf[i++] = tf->ctl;
  349. return i;
  350. }
  351. static inline void pdc20621_host_pkt(struct ata_taskfile *tf, u8 *buf,
  352. unsigned int portno)
  353. {
  354. unsigned int dw;
  355. u32 tmp, *buf32 = (u32 *) buf;
  356. unsigned int host_sg = PDC_20621_DIMM_BASE +
  357. (PDC_DIMM_WINDOW_STEP * portno) +
  358. PDC_DIMM_HOST_PRD;
  359. unsigned int dimm_sg = PDC_20621_DIMM_BASE +
  360. (PDC_DIMM_WINDOW_STEP * portno) +
  361. PDC_DIMM_HPKT_PRD;
  362. VPRINTK("ENTER, dimm_sg == 0x%x, %d\n", dimm_sg, dimm_sg);
  363. VPRINTK("host_sg == 0x%x, %d\n", host_sg, host_sg);
  364. dw = PDC_DIMM_HOST_PKT >> 2;
  365. /*
  366. * Set up Host DMA packet
  367. */
  368. if ((tf->protocol == ATA_PROT_DMA) && (!(tf->flags & ATA_TFLAG_WRITE)))
  369. tmp = PDC_PKT_READ;
  370. else
  371. tmp = 0;
  372. tmp |= ((portno + 1 + 4) << 16); /* seq. id */
  373. tmp |= (0xff << 24); /* delay seq. id */
  374. buf32[dw + 0] = cpu_to_le32(tmp);
  375. buf32[dw + 1] = cpu_to_le32(host_sg);
  376. buf32[dw + 2] = cpu_to_le32(dimm_sg);
  377. buf32[dw + 3] = 0;
  378. VPRINTK("HOST PKT @ %x == (0x%x 0x%x 0x%x 0x%x)\n",
  379. PDC_20621_DIMM_BASE + (PDC_DIMM_WINDOW_STEP * portno) +
  380. PDC_DIMM_HOST_PKT,
  381. buf32[dw + 0],
  382. buf32[dw + 1],
  383. buf32[dw + 2],
  384. buf32[dw + 3]);
  385. }
  386. static void pdc20621_dma_prep(struct ata_queued_cmd *qc)
  387. {
  388. struct scatterlist *sg;
  389. struct ata_port *ap = qc->ap;
  390. struct pdc_port_priv *pp = ap->private_data;
  391. void __iomem *mmio = ap->host_set->mmio_base;
  392. struct pdc_host_priv *hpriv = ap->host_set->private_data;
  393. void __iomem *dimm_mmio = hpriv->dimm_mmio;
  394. unsigned int portno = ap->port_no;
  395. unsigned int i, idx, total_len = 0, sgt_len;
  396. u32 *buf = (u32 *) &pp->dimm_buf[PDC_DIMM_HEADER_SZ];
  397. assert(qc->flags & ATA_QCFLAG_DMAMAP);
  398. VPRINTK("ata%u: ENTER\n", ap->id);
  399. /* hard-code chip #0 */
  400. mmio += PDC_CHIP0_OFS;
  401. /*
  402. * Build S/G table
  403. */
  404. idx = 0;
  405. ata_for_each_sg(sg, qc) {
  406. buf[idx++] = cpu_to_le32(sg_dma_address(sg));
  407. buf[idx++] = cpu_to_le32(sg_dma_len(sg));
  408. total_len += sg_dma_len(sg);
  409. }
  410. buf[idx - 1] |= cpu_to_le32(ATA_PRD_EOT);
  411. sgt_len = idx * 4;
  412. /*
  413. * Build ATA, host DMA packets
  414. */
  415. pdc20621_host_sg(&qc->tf, &pp->dimm_buf[0], portno, total_len);
  416. pdc20621_host_pkt(&qc->tf, &pp->dimm_buf[0], portno);
  417. pdc20621_ata_sg(&qc->tf, &pp->dimm_buf[0], portno, total_len);
  418. i = pdc20621_ata_pkt(&qc->tf, qc->dev->devno, &pp->dimm_buf[0], portno);
  419. if (qc->tf.flags & ATA_TFLAG_LBA48)
  420. i = pdc_prep_lba48(&qc->tf, &pp->dimm_buf[0], i);
  421. else
  422. i = pdc_prep_lba28(&qc->tf, &pp->dimm_buf[0], i);
  423. pdc_pkt_footer(&qc->tf, &pp->dimm_buf[0], i);
  424. /* copy three S/G tables and two packets to DIMM MMIO window */
  425. memcpy_toio(dimm_mmio + (portno * PDC_DIMM_WINDOW_STEP),
  426. &pp->dimm_buf, PDC_DIMM_HEADER_SZ);
  427. memcpy_toio(dimm_mmio + (portno * PDC_DIMM_WINDOW_STEP) +
  428. PDC_DIMM_HOST_PRD,
  429. &pp->dimm_buf[PDC_DIMM_HEADER_SZ], sgt_len);
  430. /* force host FIFO dump */
  431. writel(0x00000001, mmio + PDC_20621_GENERAL_CTL);
  432. readl(dimm_mmio); /* MMIO PCI posting flush */
  433. VPRINTK("ata pkt buf ofs %u, prd size %u, mmio copied\n", i, sgt_len);
  434. }
  435. static void pdc20621_nodata_prep(struct ata_queued_cmd *qc)
  436. {
  437. struct ata_port *ap = qc->ap;
  438. struct pdc_port_priv *pp = ap->private_data;
  439. void __iomem *mmio = ap->host_set->mmio_base;
  440. struct pdc_host_priv *hpriv = ap->host_set->private_data;
  441. void __iomem *dimm_mmio = hpriv->dimm_mmio;
  442. unsigned int portno = ap->port_no;
  443. unsigned int i;
  444. VPRINTK("ata%u: ENTER\n", ap->id);
  445. /* hard-code chip #0 */
  446. mmio += PDC_CHIP0_OFS;
  447. i = pdc20621_ata_pkt(&qc->tf, qc->dev->devno, &pp->dimm_buf[0], portno);
  448. if (qc->tf.flags & ATA_TFLAG_LBA48)
  449. i = pdc_prep_lba48(&qc->tf, &pp->dimm_buf[0], i);
  450. else
  451. i = pdc_prep_lba28(&qc->tf, &pp->dimm_buf[0], i);
  452. pdc_pkt_footer(&qc->tf, &pp->dimm_buf[0], i);
  453. /* copy three S/G tables and two packets to DIMM MMIO window */
  454. memcpy_toio(dimm_mmio + (portno * PDC_DIMM_WINDOW_STEP),
  455. &pp->dimm_buf, PDC_DIMM_HEADER_SZ);
  456. /* force host FIFO dump */
  457. writel(0x00000001, mmio + PDC_20621_GENERAL_CTL);
  458. readl(dimm_mmio); /* MMIO PCI posting flush */
  459. VPRINTK("ata pkt buf ofs %u, mmio copied\n", i);
  460. }
  461. static void pdc20621_qc_prep(struct ata_queued_cmd *qc)
  462. {
  463. switch (qc->tf.protocol) {
  464. case ATA_PROT_DMA:
  465. pdc20621_dma_prep(qc);
  466. break;
  467. case ATA_PROT_NODATA:
  468. pdc20621_nodata_prep(qc);
  469. break;
  470. default:
  471. break;
  472. }
  473. }
  474. static void __pdc20621_push_hdma(struct ata_queued_cmd *qc,
  475. unsigned int seq,
  476. u32 pkt_ofs)
  477. {
  478. struct ata_port *ap = qc->ap;
  479. struct ata_host_set *host_set = ap->host_set;
  480. void __iomem *mmio = host_set->mmio_base;
  481. /* hard-code chip #0 */
  482. mmio += PDC_CHIP0_OFS;
  483. writel(0x00000001, mmio + PDC_20621_SEQCTL + (seq * 4));
  484. readl(mmio + PDC_20621_SEQCTL + (seq * 4)); /* flush */
  485. writel(pkt_ofs, mmio + PDC_HDMA_PKT_SUBMIT);
  486. readl(mmio + PDC_HDMA_PKT_SUBMIT); /* flush */
  487. }
  488. static void pdc20621_push_hdma(struct ata_queued_cmd *qc,
  489. unsigned int seq,
  490. u32 pkt_ofs)
  491. {
  492. struct ata_port *ap = qc->ap;
  493. struct pdc_host_priv *pp = ap->host_set->private_data;
  494. unsigned int idx = pp->hdma_prod & PDC_HDMA_Q_MASK;
  495. if (!pp->doing_hdma) {
  496. __pdc20621_push_hdma(qc, seq, pkt_ofs);
  497. pp->doing_hdma = 1;
  498. return;
  499. }
  500. pp->hdma[idx].qc = qc;
  501. pp->hdma[idx].seq = seq;
  502. pp->hdma[idx].pkt_ofs = pkt_ofs;
  503. pp->hdma_prod++;
  504. }
  505. static void pdc20621_pop_hdma(struct ata_queued_cmd *qc)
  506. {
  507. struct ata_port *ap = qc->ap;
  508. struct pdc_host_priv *pp = ap->host_set->private_data;
  509. unsigned int idx = pp->hdma_cons & PDC_HDMA_Q_MASK;
  510. /* if nothing on queue, we're done */
  511. if (pp->hdma_prod == pp->hdma_cons) {
  512. pp->doing_hdma = 0;
  513. return;
  514. }
  515. __pdc20621_push_hdma(pp->hdma[idx].qc, pp->hdma[idx].seq,
  516. pp->hdma[idx].pkt_ofs);
  517. pp->hdma_cons++;
  518. }
  519. #ifdef ATA_VERBOSE_DEBUG
  520. static void pdc20621_dump_hdma(struct ata_queued_cmd *qc)
  521. {
  522. struct ata_port *ap = qc->ap;
  523. unsigned int port_no = ap->port_no;
  524. struct pdc_host_priv *hpriv = ap->host_set->private_data;
  525. void *dimm_mmio = hpriv->dimm_mmio;
  526. dimm_mmio += (port_no * PDC_DIMM_WINDOW_STEP);
  527. dimm_mmio += PDC_DIMM_HOST_PKT;
  528. printk(KERN_ERR "HDMA[0] == 0x%08X\n", readl(dimm_mmio));
  529. printk(KERN_ERR "HDMA[1] == 0x%08X\n", readl(dimm_mmio + 4));
  530. printk(KERN_ERR "HDMA[2] == 0x%08X\n", readl(dimm_mmio + 8));
  531. printk(KERN_ERR "HDMA[3] == 0x%08X\n", readl(dimm_mmio + 12));
  532. }
  533. #else
  534. static inline void pdc20621_dump_hdma(struct ata_queued_cmd *qc) { }
  535. #endif /* ATA_VERBOSE_DEBUG */
  536. static void pdc20621_packet_start(struct ata_queued_cmd *qc)
  537. {
  538. struct ata_port *ap = qc->ap;
  539. struct ata_host_set *host_set = ap->host_set;
  540. unsigned int port_no = ap->port_no;
  541. void __iomem *mmio = host_set->mmio_base;
  542. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  543. u8 seq = (u8) (port_no + 1);
  544. unsigned int port_ofs;
  545. /* hard-code chip #0 */
  546. mmio += PDC_CHIP0_OFS;
  547. VPRINTK("ata%u: ENTER\n", ap->id);
  548. wmb(); /* flush PRD, pkt writes */
  549. port_ofs = PDC_20621_DIMM_BASE + (PDC_DIMM_WINDOW_STEP * port_no);
  550. /* if writing, we (1) DMA to DIMM, then (2) do ATA command */
  551. if (rw && qc->tf.protocol == ATA_PROT_DMA) {
  552. seq += 4;
  553. pdc20621_dump_hdma(qc);
  554. pdc20621_push_hdma(qc, seq, port_ofs + PDC_DIMM_HOST_PKT);
  555. VPRINTK("queued ofs 0x%x (%u), seq %u\n",
  556. port_ofs + PDC_DIMM_HOST_PKT,
  557. port_ofs + PDC_DIMM_HOST_PKT,
  558. seq);
  559. } else {
  560. writel(0x00000001, mmio + PDC_20621_SEQCTL + (seq * 4));
  561. readl(mmio + PDC_20621_SEQCTL + (seq * 4)); /* flush */
  562. writel(port_ofs + PDC_DIMM_ATA_PKT,
  563. (void __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  564. readl((void __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  565. VPRINTK("submitted ofs 0x%x (%u), seq %u\n",
  566. port_ofs + PDC_DIMM_ATA_PKT,
  567. port_ofs + PDC_DIMM_ATA_PKT,
  568. seq);
  569. }
  570. }
  571. static int pdc20621_qc_issue_prot(struct ata_queued_cmd *qc)
  572. {
  573. switch (qc->tf.protocol) {
  574. case ATA_PROT_DMA:
  575. case ATA_PROT_NODATA:
  576. pdc20621_packet_start(qc);
  577. return 0;
  578. case ATA_PROT_ATAPI_DMA:
  579. BUG();
  580. break;
  581. default:
  582. break;
  583. }
  584. return ata_qc_issue_prot(qc);
  585. }
  586. static inline unsigned int pdc20621_host_intr( struct ata_port *ap,
  587. struct ata_queued_cmd *qc,
  588. unsigned int doing_hdma,
  589. void __iomem *mmio)
  590. {
  591. unsigned int port_no = ap->port_no;
  592. unsigned int port_ofs =
  593. PDC_20621_DIMM_BASE + (PDC_DIMM_WINDOW_STEP * port_no);
  594. u8 status;
  595. unsigned int handled = 0;
  596. VPRINTK("ENTER\n");
  597. if ((qc->tf.protocol == ATA_PROT_DMA) && /* read */
  598. (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
  599. /* step two - DMA from DIMM to host */
  600. if (doing_hdma) {
  601. VPRINTK("ata%u: read hdma, 0x%x 0x%x\n", ap->id,
  602. readl(mmio + 0x104), readl(mmio + PDC_HDMA_CTLSTAT));
  603. /* get drive status; clear intr; complete txn */
  604. qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
  605. ata_qc_complete(qc);
  606. pdc20621_pop_hdma(qc);
  607. }
  608. /* step one - exec ATA command */
  609. else {
  610. u8 seq = (u8) (port_no + 1 + 4);
  611. VPRINTK("ata%u: read ata, 0x%x 0x%x\n", ap->id,
  612. readl(mmio + 0x104), readl(mmio + PDC_HDMA_CTLSTAT));
  613. /* submit hdma pkt */
  614. pdc20621_dump_hdma(qc);
  615. pdc20621_push_hdma(qc, seq,
  616. port_ofs + PDC_DIMM_HOST_PKT);
  617. }
  618. handled = 1;
  619. } else if (qc->tf.protocol == ATA_PROT_DMA) { /* write */
  620. /* step one - DMA from host to DIMM */
  621. if (doing_hdma) {
  622. u8 seq = (u8) (port_no + 1);
  623. VPRINTK("ata%u: write hdma, 0x%x 0x%x\n", ap->id,
  624. readl(mmio + 0x104), readl(mmio + PDC_HDMA_CTLSTAT));
  625. /* submit ata pkt */
  626. writel(0x00000001, mmio + PDC_20621_SEQCTL + (seq * 4));
  627. readl(mmio + PDC_20621_SEQCTL + (seq * 4));
  628. writel(port_ofs + PDC_DIMM_ATA_PKT,
  629. (void __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  630. readl((void __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  631. }
  632. /* step two - execute ATA command */
  633. else {
  634. VPRINTK("ata%u: write ata, 0x%x 0x%x\n", ap->id,
  635. readl(mmio + 0x104), readl(mmio + PDC_HDMA_CTLSTAT));
  636. /* get drive status; clear intr; complete txn */
  637. qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
  638. ata_qc_complete(qc);
  639. pdc20621_pop_hdma(qc);
  640. }
  641. handled = 1;
  642. /* command completion, but no data xfer */
  643. } else if (qc->tf.protocol == ATA_PROT_NODATA) {
  644. status = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
  645. DPRINTK("BUS_NODATA (drv_stat 0x%X)\n", status);
  646. qc->err_mask |= ac_err_mask(status);
  647. ata_qc_complete(qc);
  648. handled = 1;
  649. } else {
  650. ap->stats.idle_irq++;
  651. }
  652. return handled;
  653. }
  654. static void pdc20621_irq_clear(struct ata_port *ap)
  655. {
  656. struct ata_host_set *host_set = ap->host_set;
  657. void __iomem *mmio = host_set->mmio_base;
  658. mmio += PDC_CHIP0_OFS;
  659. readl(mmio + PDC_20621_SEQMASK);
  660. }
  661. static irqreturn_t pdc20621_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
  662. {
  663. struct ata_host_set *host_set = dev_instance;
  664. struct ata_port *ap;
  665. u32 mask = 0;
  666. unsigned int i, tmp, port_no;
  667. unsigned int handled = 0;
  668. void __iomem *mmio_base;
  669. VPRINTK("ENTER\n");
  670. if (!host_set || !host_set->mmio_base) {
  671. VPRINTK("QUICK EXIT\n");
  672. return IRQ_NONE;
  673. }
  674. mmio_base = host_set->mmio_base;
  675. /* reading should also clear interrupts */
  676. mmio_base += PDC_CHIP0_OFS;
  677. mask = readl(mmio_base + PDC_20621_SEQMASK);
  678. VPRINTK("mask == 0x%x\n", mask);
  679. if (mask == 0xffffffff) {
  680. VPRINTK("QUICK EXIT 2\n");
  681. return IRQ_NONE;
  682. }
  683. mask &= 0xffff; /* only 16 tags possible */
  684. if (!mask) {
  685. VPRINTK("QUICK EXIT 3\n");
  686. return IRQ_NONE;
  687. }
  688. spin_lock(&host_set->lock);
  689. for (i = 1; i < 9; i++) {
  690. port_no = i - 1;
  691. if (port_no > 3)
  692. port_no -= 4;
  693. if (port_no >= host_set->n_ports)
  694. ap = NULL;
  695. else
  696. ap = host_set->ports[port_no];
  697. tmp = mask & (1 << i);
  698. VPRINTK("seq %u, port_no %u, ap %p, tmp %x\n", i, port_no, ap, tmp);
  699. if (tmp && ap &&
  700. !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
  701. struct ata_queued_cmd *qc;
  702. qc = ata_qc_from_tag(ap, ap->active_tag);
  703. if (qc && (!(qc->tf.ctl & ATA_NIEN)))
  704. handled += pdc20621_host_intr(ap, qc, (i > 4),
  705. mmio_base);
  706. }
  707. }
  708. spin_unlock(&host_set->lock);
  709. VPRINTK("mask == 0x%x\n", mask);
  710. VPRINTK("EXIT\n");
  711. return IRQ_RETVAL(handled);
  712. }
  713. static void pdc_eng_timeout(struct ata_port *ap)
  714. {
  715. u8 drv_stat;
  716. struct ata_host_set *host_set = ap->host_set;
  717. struct ata_queued_cmd *qc;
  718. unsigned long flags;
  719. DPRINTK("ENTER\n");
  720. spin_lock_irqsave(&host_set->lock, flags);
  721. qc = ata_qc_from_tag(ap, ap->active_tag);
  722. if (!qc) {
  723. printk(KERN_ERR "ata%u: BUG: timeout without command\n",
  724. ap->id);
  725. goto out;
  726. }
  727. /* hack alert! We cannot use the supplied completion
  728. * function from inside the ->eh_strategy_handler() thread.
  729. * libata is the only user of ->eh_strategy_handler() in
  730. * any kernel, so the default scsi_done() assumes it is
  731. * not being called from the SCSI EH.
  732. */
  733. qc->scsidone = scsi_finish_command;
  734. switch (qc->tf.protocol) {
  735. case ATA_PROT_DMA:
  736. case ATA_PROT_NODATA:
  737. printk(KERN_ERR "ata%u: command timeout\n", ap->id);
  738. qc->err_mask |= __ac_err_mask(ata_wait_idle(ap));
  739. ata_qc_complete(qc);
  740. break;
  741. default:
  742. drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
  743. printk(KERN_ERR "ata%u: unknown timeout, cmd 0x%x stat 0x%x\n",
  744. ap->id, qc->tf.command, drv_stat);
  745. qc->err_mask |= ac_err_mask(drv_stat);
  746. ata_qc_complete(qc);
  747. break;
  748. }
  749. out:
  750. spin_unlock_irqrestore(&host_set->lock, flags);
  751. DPRINTK("EXIT\n");
  752. }
  753. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  754. {
  755. WARN_ON (tf->protocol == ATA_PROT_DMA ||
  756. tf->protocol == ATA_PROT_NODATA);
  757. ata_tf_load(ap, tf);
  758. }
  759. static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  760. {
  761. WARN_ON (tf->protocol == ATA_PROT_DMA ||
  762. tf->protocol == ATA_PROT_NODATA);
  763. ata_exec_command(ap, tf);
  764. }
  765. static void pdc_sata_setup_port(struct ata_ioports *port, unsigned long base)
  766. {
  767. port->cmd_addr = base;
  768. port->data_addr = base;
  769. port->feature_addr =
  770. port->error_addr = base + 0x4;
  771. port->nsect_addr = base + 0x8;
  772. port->lbal_addr = base + 0xc;
  773. port->lbam_addr = base + 0x10;
  774. port->lbah_addr = base + 0x14;
  775. port->device_addr = base + 0x18;
  776. port->command_addr =
  777. port->status_addr = base + 0x1c;
  778. port->altstatus_addr =
  779. port->ctl_addr = base + 0x38;
  780. }
  781. #ifdef ATA_VERBOSE_DEBUG
  782. static void pdc20621_get_from_dimm(struct ata_probe_ent *pe, void *psource,
  783. u32 offset, u32 size)
  784. {
  785. u32 window_size;
  786. u16 idx;
  787. u8 page_mask;
  788. long dist;
  789. void __iomem *mmio = pe->mmio_base;
  790. struct pdc_host_priv *hpriv = pe->private_data;
  791. void __iomem *dimm_mmio = hpriv->dimm_mmio;
  792. /* hard-code chip #0 */
  793. mmio += PDC_CHIP0_OFS;
  794. page_mask = 0x00;
  795. window_size = 0x2000 * 4; /* 32K byte uchar size */
  796. idx = (u16) (offset / window_size);
  797. writel(0x01, mmio + PDC_GENERAL_CTLR);
  798. readl(mmio + PDC_GENERAL_CTLR);
  799. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  800. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  801. offset -= (idx * window_size);
  802. idx++;
  803. dist = ((long) (window_size - (offset + size))) >= 0 ? size :
  804. (long) (window_size - offset);
  805. memcpy_fromio((char *) psource, (char *) (dimm_mmio + offset / 4),
  806. dist);
  807. psource += dist;
  808. size -= dist;
  809. for (; (long) size >= (long) window_size ;) {
  810. writel(0x01, mmio + PDC_GENERAL_CTLR);
  811. readl(mmio + PDC_GENERAL_CTLR);
  812. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  813. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  814. memcpy_fromio((char *) psource, (char *) (dimm_mmio),
  815. window_size / 4);
  816. psource += window_size;
  817. size -= window_size;
  818. idx ++;
  819. }
  820. if (size) {
  821. writel(0x01, mmio + PDC_GENERAL_CTLR);
  822. readl(mmio + PDC_GENERAL_CTLR);
  823. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  824. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  825. memcpy_fromio((char *) psource, (char *) (dimm_mmio),
  826. size / 4);
  827. }
  828. }
  829. #endif
  830. static void pdc20621_put_to_dimm(struct ata_probe_ent *pe, void *psource,
  831. u32 offset, u32 size)
  832. {
  833. u32 window_size;
  834. u16 idx;
  835. u8 page_mask;
  836. long dist;
  837. void __iomem *mmio = pe->mmio_base;
  838. struct pdc_host_priv *hpriv = pe->private_data;
  839. void __iomem *dimm_mmio = hpriv->dimm_mmio;
  840. /* hard-code chip #0 */
  841. mmio += PDC_CHIP0_OFS;
  842. page_mask = 0x00;
  843. window_size = 0x2000 * 4; /* 32K byte uchar size */
  844. idx = (u16) (offset / window_size);
  845. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  846. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  847. offset -= (idx * window_size);
  848. idx++;
  849. dist = ((long)(s32)(window_size - (offset + size))) >= 0 ? size :
  850. (long) (window_size - offset);
  851. memcpy_toio(dimm_mmio + offset / 4, psource, dist);
  852. writel(0x01, mmio + PDC_GENERAL_CTLR);
  853. readl(mmio + PDC_GENERAL_CTLR);
  854. psource += dist;
  855. size -= dist;
  856. for (; (long) size >= (long) window_size ;) {
  857. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  858. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  859. memcpy_toio(dimm_mmio, psource, window_size / 4);
  860. writel(0x01, mmio + PDC_GENERAL_CTLR);
  861. readl(mmio + PDC_GENERAL_CTLR);
  862. psource += window_size;
  863. size -= window_size;
  864. idx ++;
  865. }
  866. if (size) {
  867. writel(((idx) << page_mask), mmio + PDC_DIMM_WINDOW_CTLR);
  868. readl(mmio + PDC_DIMM_WINDOW_CTLR);
  869. memcpy_toio(dimm_mmio, psource, size / 4);
  870. writel(0x01, mmio + PDC_GENERAL_CTLR);
  871. readl(mmio + PDC_GENERAL_CTLR);
  872. }
  873. }
  874. static unsigned int pdc20621_i2c_read(struct ata_probe_ent *pe, u32 device,
  875. u32 subaddr, u32 *pdata)
  876. {
  877. void __iomem *mmio = pe->mmio_base;
  878. u32 i2creg = 0;
  879. u32 status;
  880. u32 count =0;
  881. /* hard-code chip #0 */
  882. mmio += PDC_CHIP0_OFS;
  883. i2creg |= device << 24;
  884. i2creg |= subaddr << 16;
  885. /* Set the device and subaddress */
  886. writel(i2creg, mmio + PDC_I2C_ADDR_DATA_OFFSET);
  887. readl(mmio + PDC_I2C_ADDR_DATA_OFFSET);
  888. /* Write Control to perform read operation, mask int */
  889. writel(PDC_I2C_READ | PDC_I2C_START | PDC_I2C_MASK_INT,
  890. mmio + PDC_I2C_CONTROL_OFFSET);
  891. for (count = 0; count <= 1000; count ++) {
  892. status = readl(mmio + PDC_I2C_CONTROL_OFFSET);
  893. if (status & PDC_I2C_COMPLETE) {
  894. status = readl(mmio + PDC_I2C_ADDR_DATA_OFFSET);
  895. break;
  896. } else if (count == 1000)
  897. return 0;
  898. }
  899. *pdata = (status >> 8) & 0x000000ff;
  900. return 1;
  901. }
  902. static int pdc20621_detect_dimm(struct ata_probe_ent *pe)
  903. {
  904. u32 data=0 ;
  905. if (pdc20621_i2c_read(pe, PDC_DIMM0_SPD_DEV_ADDRESS,
  906. PDC_DIMM_SPD_SYSTEM_FREQ, &data)) {
  907. if (data == 100)
  908. return 100;
  909. } else
  910. return 0;
  911. if (pdc20621_i2c_read(pe, PDC_DIMM0_SPD_DEV_ADDRESS, 9, &data)) {
  912. if(data <= 0x75)
  913. return 133;
  914. } else
  915. return 0;
  916. return 0;
  917. }
  918. static int pdc20621_prog_dimm0(struct ata_probe_ent *pe)
  919. {
  920. u32 spd0[50];
  921. u32 data = 0;
  922. int size, i;
  923. u8 bdimmsize;
  924. void __iomem *mmio = pe->mmio_base;
  925. static const struct {
  926. unsigned int reg;
  927. unsigned int ofs;
  928. } pdc_i2c_read_data [] = {
  929. { PDC_DIMM_SPD_TYPE, 11 },
  930. { PDC_DIMM_SPD_FRESH_RATE, 12 },
  931. { PDC_DIMM_SPD_COLUMN_NUM, 4 },
  932. { PDC_DIMM_SPD_ATTRIBUTE, 21 },
  933. { PDC_DIMM_SPD_ROW_NUM, 3 },
  934. { PDC_DIMM_SPD_BANK_NUM, 17 },
  935. { PDC_DIMM_SPD_MODULE_ROW, 5 },
  936. { PDC_DIMM_SPD_ROW_PRE_CHARGE, 27 },
  937. { PDC_DIMM_SPD_ROW_ACTIVE_DELAY, 28 },
  938. { PDC_DIMM_SPD_RAS_CAS_DELAY, 29 },
  939. { PDC_DIMM_SPD_ACTIVE_PRECHARGE, 30 },
  940. { PDC_DIMM_SPD_CAS_LATENCY, 18 },
  941. };
  942. /* hard-code chip #0 */
  943. mmio += PDC_CHIP0_OFS;
  944. for(i=0; i<ARRAY_SIZE(pdc_i2c_read_data); i++)
  945. pdc20621_i2c_read(pe, PDC_DIMM0_SPD_DEV_ADDRESS,
  946. pdc_i2c_read_data[i].reg,
  947. &spd0[pdc_i2c_read_data[i].ofs]);
  948. data |= (spd0[4] - 8) | ((spd0[21] != 0) << 3) | ((spd0[3]-11) << 4);
  949. data |= ((spd0[17] / 4) << 6) | ((spd0[5] / 2) << 7) |
  950. ((((spd0[27] + 9) / 10) - 1) << 8) ;
  951. data |= (((((spd0[29] > spd0[28])
  952. ? spd0[29] : spd0[28]) + 9) / 10) - 1) << 10;
  953. data |= ((spd0[30] - spd0[29] + 9) / 10 - 2) << 12;
  954. if (spd0[18] & 0x08)
  955. data |= ((0x03) << 14);
  956. else if (spd0[18] & 0x04)
  957. data |= ((0x02) << 14);
  958. else if (spd0[18] & 0x01)
  959. data |= ((0x01) << 14);
  960. else
  961. data |= (0 << 14);
  962. /*
  963. Calculate the size of bDIMMSize (power of 2) and
  964. merge the DIMM size by program start/end address.
  965. */
  966. bdimmsize = spd0[4] + (spd0[5] / 2) + spd0[3] + (spd0[17] / 2) + 3;
  967. size = (1 << bdimmsize) >> 20; /* size = xxx(MB) */
  968. data |= (((size / 16) - 1) << 16);
  969. data |= (0 << 23);
  970. data |= 8;
  971. writel(data, mmio + PDC_DIMM0_CONTROL_OFFSET);
  972. readl(mmio + PDC_DIMM0_CONTROL_OFFSET);
  973. return size;
  974. }
  975. static unsigned int pdc20621_prog_dimm_global(struct ata_probe_ent *pe)
  976. {
  977. u32 data, spd0;
  978. int error, i;
  979. void __iomem *mmio = pe->mmio_base;
  980. /* hard-code chip #0 */
  981. mmio += PDC_CHIP0_OFS;
  982. /*
  983. Set To Default : DIMM Module Global Control Register (0x022259F1)
  984. DIMM Arbitration Disable (bit 20)
  985. DIMM Data/Control Output Driving Selection (bit12 - bit15)
  986. Refresh Enable (bit 17)
  987. */
  988. data = 0x022259F1;
  989. writel(data, mmio + PDC_SDRAM_CONTROL_OFFSET);
  990. readl(mmio + PDC_SDRAM_CONTROL_OFFSET);
  991. /* Turn on for ECC */
  992. pdc20621_i2c_read(pe, PDC_DIMM0_SPD_DEV_ADDRESS,
  993. PDC_DIMM_SPD_TYPE, &spd0);
  994. if (spd0 == 0x02) {
  995. data |= (0x01 << 16);
  996. writel(data, mmio + PDC_SDRAM_CONTROL_OFFSET);
  997. readl(mmio + PDC_SDRAM_CONTROL_OFFSET);
  998. printk(KERN_ERR "Local DIMM ECC Enabled\n");
  999. }
  1000. /* DIMM Initialization Select/Enable (bit 18/19) */
  1001. data &= (~(1<<18));
  1002. data |= (1<<19);
  1003. writel(data, mmio + PDC_SDRAM_CONTROL_OFFSET);
  1004. error = 1;
  1005. for (i = 1; i <= 10; i++) { /* polling ~5 secs */
  1006. data = readl(mmio + PDC_SDRAM_CONTROL_OFFSET);
  1007. if (!(data & (1<<19))) {
  1008. error = 0;
  1009. break;
  1010. }
  1011. msleep(i*100);
  1012. }
  1013. return error;
  1014. }
  1015. static unsigned int pdc20621_dimm_init(struct ata_probe_ent *pe)
  1016. {
  1017. int speed, size, length;
  1018. u32 addr,spd0,pci_status;
  1019. u32 tmp=0;
  1020. u32 time_period=0;
  1021. u32 tcount=0;
  1022. u32 ticks=0;
  1023. u32 clock=0;
  1024. u32 fparam=0;
  1025. void __iomem *mmio = pe->mmio_base;
  1026. /* hard-code chip #0 */
  1027. mmio += PDC_CHIP0_OFS;
  1028. /* Initialize PLL based upon PCI Bus Frequency */
  1029. /* Initialize Time Period Register */
  1030. writel(0xffffffff, mmio + PDC_TIME_PERIOD);
  1031. time_period = readl(mmio + PDC_TIME_PERIOD);
  1032. VPRINTK("Time Period Register (0x40): 0x%x\n", time_period);
  1033. /* Enable timer */
  1034. writel(0x00001a0, mmio + PDC_TIME_CONTROL);
  1035. readl(mmio + PDC_TIME_CONTROL);
  1036. /* Wait 3 seconds */
  1037. msleep(3000);
  1038. /*
  1039. When timer is enabled, counter is decreased every internal
  1040. clock cycle.
  1041. */
  1042. tcount = readl(mmio + PDC_TIME_COUNTER);
  1043. VPRINTK("Time Counter Register (0x44): 0x%x\n", tcount);
  1044. /*
  1045. If SX4 is on PCI-X bus, after 3 seconds, the timer counter
  1046. register should be >= (0xffffffff - 3x10^8).
  1047. */
  1048. if(tcount >= PCI_X_TCOUNT) {
  1049. ticks = (time_period - tcount);
  1050. VPRINTK("Num counters 0x%x (%d)\n", ticks, ticks);
  1051. clock = (ticks / 300000);
  1052. VPRINTK("10 * Internal clk = 0x%x (%d)\n", clock, clock);
  1053. clock = (clock * 33);
  1054. VPRINTK("10 * Internal clk * 33 = 0x%x (%d)\n", clock, clock);
  1055. /* PLL F Param (bit 22:16) */
  1056. fparam = (1400000 / clock) - 2;
  1057. VPRINTK("PLL F Param: 0x%x (%d)\n", fparam, fparam);
  1058. /* OD param = 0x2 (bit 31:30), R param = 0x5 (bit 29:25) */
  1059. pci_status = (0x8a001824 | (fparam << 16));
  1060. } else
  1061. pci_status = PCI_PLL_INIT;
  1062. /* Initialize PLL. */
  1063. VPRINTK("pci_status: 0x%x\n", pci_status);
  1064. writel(pci_status, mmio + PDC_CTL_STATUS);
  1065. readl(mmio + PDC_CTL_STATUS);
  1066. /*
  1067. Read SPD of DIMM by I2C interface,
  1068. and program the DIMM Module Controller.
  1069. */
  1070. if (!(speed = pdc20621_detect_dimm(pe))) {
  1071. printk(KERN_ERR "Detect Local DIMM Fail\n");
  1072. return 1; /* DIMM error */
  1073. }
  1074. VPRINTK("Local DIMM Speed = %d\n", speed);
  1075. /* Programming DIMM0 Module Control Register (index_CID0:80h) */
  1076. size = pdc20621_prog_dimm0(pe);
  1077. VPRINTK("Local DIMM Size = %dMB\n",size);
  1078. /* Programming DIMM Module Global Control Register (index_CID0:88h) */
  1079. if (pdc20621_prog_dimm_global(pe)) {
  1080. printk(KERN_ERR "Programming DIMM Module Global Control Register Fail\n");
  1081. return 1;
  1082. }
  1083. #ifdef ATA_VERBOSE_DEBUG
  1084. {
  1085. u8 test_parttern1[40] = {0x55,0xAA,'P','r','o','m','i','s','e',' ',
  1086. 'N','o','t',' ','Y','e','t',' ','D','e','f','i','n','e','d',' ',
  1087. '1','.','1','0',
  1088. '9','8','0','3','1','6','1','2',0,0};
  1089. u8 test_parttern2[40] = {0};
  1090. pdc20621_put_to_dimm(pe, (void *) test_parttern2, 0x10040, 40);
  1091. pdc20621_put_to_dimm(pe, (void *) test_parttern2, 0x40, 40);
  1092. pdc20621_put_to_dimm(pe, (void *) test_parttern1, 0x10040, 40);
  1093. pdc20621_get_from_dimm(pe, (void *) test_parttern2, 0x40, 40);
  1094. printk(KERN_ERR "%x, %x, %s\n", test_parttern2[0],
  1095. test_parttern2[1], &(test_parttern2[2]));
  1096. pdc20621_get_from_dimm(pe, (void *) test_parttern2, 0x10040,
  1097. 40);
  1098. printk(KERN_ERR "%x, %x, %s\n", test_parttern2[0],
  1099. test_parttern2[1], &(test_parttern2[2]));
  1100. pdc20621_put_to_dimm(pe, (void *) test_parttern1, 0x40, 40);
  1101. pdc20621_get_from_dimm(pe, (void *) test_parttern2, 0x40, 40);
  1102. printk(KERN_ERR "%x, %x, %s\n", test_parttern2[0],
  1103. test_parttern2[1], &(test_parttern2[2]));
  1104. }
  1105. #endif
  1106. /* ECC initiliazation. */
  1107. pdc20621_i2c_read(pe, PDC_DIMM0_SPD_DEV_ADDRESS,
  1108. PDC_DIMM_SPD_TYPE, &spd0);
  1109. if (spd0 == 0x02) {
  1110. VPRINTK("Start ECC initialization\n");
  1111. addr = 0;
  1112. length = size * 1024 * 1024;
  1113. while (addr < length) {
  1114. pdc20621_put_to_dimm(pe, (void *) &tmp, addr,
  1115. sizeof(u32));
  1116. addr += sizeof(u32);
  1117. }
  1118. VPRINTK("Finish ECC initialization\n");
  1119. }
  1120. return 0;
  1121. }
  1122. static void pdc_20621_init(struct ata_probe_ent *pe)
  1123. {
  1124. u32 tmp;
  1125. void __iomem *mmio = pe->mmio_base;
  1126. /* hard-code chip #0 */
  1127. mmio += PDC_CHIP0_OFS;
  1128. /*
  1129. * Select page 0x40 for our 32k DIMM window
  1130. */
  1131. tmp = readl(mmio + PDC_20621_DIMM_WINDOW) & 0xffff0000;
  1132. tmp |= PDC_PAGE_WINDOW; /* page 40h; arbitrarily selected */
  1133. writel(tmp, mmio + PDC_20621_DIMM_WINDOW);
  1134. /*
  1135. * Reset Host DMA
  1136. */
  1137. tmp = readl(mmio + PDC_HDMA_CTLSTAT);
  1138. tmp |= PDC_RESET;
  1139. writel(tmp, mmio + PDC_HDMA_CTLSTAT);
  1140. readl(mmio + PDC_HDMA_CTLSTAT); /* flush */
  1141. udelay(10);
  1142. tmp = readl(mmio + PDC_HDMA_CTLSTAT);
  1143. tmp &= ~PDC_RESET;
  1144. writel(tmp, mmio + PDC_HDMA_CTLSTAT);
  1145. readl(mmio + PDC_HDMA_CTLSTAT); /* flush */
  1146. }
  1147. static int pdc_sata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  1148. {
  1149. static int printed_version;
  1150. struct ata_probe_ent *probe_ent = NULL;
  1151. unsigned long base;
  1152. void __iomem *mmio_base;
  1153. void __iomem *dimm_mmio = NULL;
  1154. struct pdc_host_priv *hpriv = NULL;
  1155. unsigned int board_idx = (unsigned int) ent->driver_data;
  1156. int pci_dev_busy = 0;
  1157. int rc;
  1158. if (!printed_version++)
  1159. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  1160. /*
  1161. * If this driver happens to only be useful on Apple's K2, then
  1162. * we should check that here as it has a normal Serverworks ID
  1163. */
  1164. rc = pci_enable_device(pdev);
  1165. if (rc)
  1166. return rc;
  1167. rc = pci_request_regions(pdev, DRV_NAME);
  1168. if (rc) {
  1169. pci_dev_busy = 1;
  1170. goto err_out;
  1171. }
  1172. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  1173. if (rc)
  1174. goto err_out_regions;
  1175. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  1176. if (rc)
  1177. goto err_out_regions;
  1178. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  1179. if (probe_ent == NULL) {
  1180. rc = -ENOMEM;
  1181. goto err_out_regions;
  1182. }
  1183. memset(probe_ent, 0, sizeof(*probe_ent));
  1184. probe_ent->dev = pci_dev_to_dev(pdev);
  1185. INIT_LIST_HEAD(&probe_ent->node);
  1186. mmio_base = pci_iomap(pdev, 3, 0);
  1187. if (mmio_base == NULL) {
  1188. rc = -ENOMEM;
  1189. goto err_out_free_ent;
  1190. }
  1191. base = (unsigned long) mmio_base;
  1192. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  1193. if (!hpriv) {
  1194. rc = -ENOMEM;
  1195. goto err_out_iounmap;
  1196. }
  1197. memset(hpriv, 0, sizeof(*hpriv));
  1198. dimm_mmio = pci_iomap(pdev, 4, 0);
  1199. if (!dimm_mmio) {
  1200. kfree(hpriv);
  1201. rc = -ENOMEM;
  1202. goto err_out_iounmap;
  1203. }
  1204. hpriv->dimm_mmio = dimm_mmio;
  1205. probe_ent->sht = pdc_port_info[board_idx].sht;
  1206. probe_ent->host_flags = pdc_port_info[board_idx].host_flags;
  1207. probe_ent->pio_mask = pdc_port_info[board_idx].pio_mask;
  1208. probe_ent->mwdma_mask = pdc_port_info[board_idx].mwdma_mask;
  1209. probe_ent->udma_mask = pdc_port_info[board_idx].udma_mask;
  1210. probe_ent->port_ops = pdc_port_info[board_idx].port_ops;
  1211. probe_ent->irq = pdev->irq;
  1212. probe_ent->irq_flags = SA_SHIRQ;
  1213. probe_ent->mmio_base = mmio_base;
  1214. probe_ent->private_data = hpriv;
  1215. base += PDC_CHIP0_OFS;
  1216. probe_ent->n_ports = 4;
  1217. pdc_sata_setup_port(&probe_ent->port[0], base + 0x200);
  1218. pdc_sata_setup_port(&probe_ent->port[1], base + 0x280);
  1219. pdc_sata_setup_port(&probe_ent->port[2], base + 0x300);
  1220. pdc_sata_setup_port(&probe_ent->port[3], base + 0x380);
  1221. pci_set_master(pdev);
  1222. /* initialize adapter */
  1223. /* initialize local dimm */
  1224. if (pdc20621_dimm_init(probe_ent)) {
  1225. rc = -ENOMEM;
  1226. goto err_out_iounmap_dimm;
  1227. }
  1228. pdc_20621_init(probe_ent);
  1229. /* FIXME: check ata_device_add return value */
  1230. ata_device_add(probe_ent);
  1231. kfree(probe_ent);
  1232. return 0;
  1233. err_out_iounmap_dimm: /* only get to this label if 20621 */
  1234. kfree(hpriv);
  1235. pci_iounmap(pdev, dimm_mmio);
  1236. err_out_iounmap:
  1237. pci_iounmap(pdev, mmio_base);
  1238. err_out_free_ent:
  1239. kfree(probe_ent);
  1240. err_out_regions:
  1241. pci_release_regions(pdev);
  1242. err_out:
  1243. if (!pci_dev_busy)
  1244. pci_disable_device(pdev);
  1245. return rc;
  1246. }
  1247. static int __init pdc_sata_init(void)
  1248. {
  1249. return pci_module_init(&pdc_sata_pci_driver);
  1250. }
  1251. static void __exit pdc_sata_exit(void)
  1252. {
  1253. pci_unregister_driver(&pdc_sata_pci_driver);
  1254. }
  1255. MODULE_AUTHOR("Jeff Garzik");
  1256. MODULE_DESCRIPTION("Promise SATA low-level driver");
  1257. MODULE_LICENSE("GPL");
  1258. MODULE_DEVICE_TABLE(pci, pdc_sata_pci_tbl);
  1259. MODULE_VERSION(DRV_VERSION);
  1260. module_init(pdc_sata_init);
  1261. module_exit(pdc_sata_exit);