sata_sx4.c 40 KB

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