hpt366.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  1. /*
  2. * Copyright (C) 1999-2003 Andre Hedrick <andre@linux-ide.org>
  3. * Portions Copyright (C) 2001 Sun Microsystems, Inc.
  4. * Portions Copyright (C) 2003 Red Hat Inc
  5. * Portions Copyright (C) 2007 Bartlomiej Zolnierkiewicz
  6. * Portions Copyright (C) 2005-2009 MontaVista Software, Inc.
  7. *
  8. * Thanks to HighPoint Technologies for their assistance, and hardware.
  9. * Special Thanks to Jon Burchmore in SanDiego for the deep pockets, his
  10. * donation of an ABit BP6 mainboard, processor, and memory acellerated
  11. * development and support.
  12. *
  13. *
  14. * HighPoint has its own drivers (open source except for the RAID part)
  15. * available from http://www.highpoint-tech.com/USA_new/service_support.htm
  16. * This may be useful to anyone wanting to work on this driver, however do not
  17. * trust them too much since the code tends to become less and less meaningful
  18. * as the time passes... :-/
  19. *
  20. * Note that final HPT370 support was done by force extraction of GPL.
  21. *
  22. * - add function for getting/setting power status of drive
  23. * - the HPT370's state machine can get confused. reset it before each dma
  24. * xfer to prevent that from happening.
  25. * - reset state engine whenever we get an error.
  26. * - check for busmaster state at end of dma.
  27. * - use new highpoint timings.
  28. * - detect bus speed using highpoint register.
  29. * - use pll if we don't have a clock table. added a 66MHz table that's
  30. * just 2x the 33MHz table.
  31. * - removed turnaround. NOTE: we never want to switch between pll and
  32. * pci clocks as the chip can glitch in those cases. the highpoint
  33. * approved workaround slows everything down too much to be useful. in
  34. * addition, we would have to serialize access to each chip.
  35. * Adrian Sun <a.sun@sun.com>
  36. *
  37. * add drive timings for 66MHz PCI bus,
  38. * fix ATA Cable signal detection, fix incorrect /proc info
  39. * add /proc display for per-drive PIO/DMA/UDMA mode and
  40. * per-channel ATA-33/66 Cable detect.
  41. * Duncan Laurie <void@sun.com>
  42. *
  43. * fixup /proc output for multiple controllers
  44. * Tim Hockin <thockin@sun.com>
  45. *
  46. * On hpt366:
  47. * Reset the hpt366 on error, reset on dma
  48. * Fix disabling Fast Interrupt hpt366.
  49. * Mike Waychison <crlf@sun.com>
  50. *
  51. * Added support for 372N clocking and clock switching. The 372N needs
  52. * different clocks on read/write. This requires overloading rw_disk and
  53. * other deeply crazy things. Thanks to <http://www.hoerstreich.de> for
  54. * keeping me sane.
  55. * Alan Cox <alan@lxorguk.ukuu.org.uk>
  56. *
  57. * - fix the clock turnaround code: it was writing to the wrong ports when
  58. * called for the secondary channel, caching the current clock mode per-
  59. * channel caused the cached register value to get out of sync with the
  60. * actual one, the channels weren't serialized, the turnaround shouldn't
  61. * be done on 66 MHz PCI bus
  62. * - disable UltraATA/100 for HPT370 by default as the 33 MHz clock being used
  63. * does not allow for this speed anyway
  64. * - avoid touching disabled channels (e.g. HPT371/N are single channel chips,
  65. * their primary channel is kind of virtual, it isn't tied to any pins)
  66. * - fix/remove bad/unused timing tables and use one set of tables for the whole
  67. * HPT37x chip family; save space by introducing the separate transfer mode
  68. * table in which the mode lookup is done
  69. * - use f_CNT value saved by the HighPoint BIOS as reading it directly gives
  70. * the wrong PCI frequency since DPLL has already been calibrated by BIOS;
  71. * read it only from the function 0 of HPT374 chips
  72. * - fix the hotswap code: it caused RESET- to glitch when tristating the bus,
  73. * and for HPT36x the obsolete HDIO_TRISTATE_HWIF handler was called instead
  74. * - pass to init_chipset() handlers a copy of the IDE PCI device structure as
  75. * they tamper with its fields
  76. * - pass to the init_setup handlers a copy of the ide_pci_device_t structure
  77. * since they may tamper with its fields
  78. * - prefix the driver startup messages with the real chip name
  79. * - claim the extra 240 bytes of I/O space for all chips
  80. * - optimize the UltraDMA filtering and the drive list lookup code
  81. * - use pci_get_slot() to get to the function 1 of HPT36x/374
  82. * - cache offset of the channel's misc. control registers (MCRs) being used
  83. * throughout the driver
  84. * - only touch the relevant MCR when detecting the cable type on HPT374's
  85. * function 1
  86. * - rename all the register related variables consistently
  87. * - move all the interrupt twiddling code from the speedproc handlers into
  88. * init_hwif_hpt366(), also grouping all the DMA related code together there
  89. * - merge HPT36x/HPT37x speedproc handlers, fix PIO timing register mask and
  90. * separate the UltraDMA and MWDMA masks there to avoid changing PIO timings
  91. * when setting an UltraDMA mode
  92. * - fix hpt3xx_tune_drive() to set the PIO mode requested, not always select
  93. * the best possible one
  94. * - clean up DMA timeout handling for HPT370
  95. * - switch to using the enumeration type to differ between the numerous chip
  96. * variants, matching PCI device/revision ID with the chip type early, at the
  97. * init_setup stage
  98. * - extend the hpt_info structure to hold the DPLL and PCI clock frequencies,
  99. * stop duplicating it for each channel by storing the pointer in the pci_dev
  100. * structure: first, at the init_setup stage, point it to a static "template"
  101. * with only the chip type and its specific base DPLL frequency, the highest
  102. * UltraDMA mode, and the chip settings table pointer filled, then, at the
  103. * init_chipset stage, allocate per-chip instance and fill it with the rest
  104. * of the necessary information
  105. * - get rid of the constant thresholds in the HPT37x PCI clock detection code,
  106. * switch to calculating PCI clock frequency based on the chip's base DPLL
  107. * frequency
  108. * - switch to using the DPLL clock and enable UltraATA/133 mode by default on
  109. * anything newer than HPT370/A (except HPT374 that is not capable of this
  110. * mode according to the manual)
  111. * - fold PCI clock detection and DPLL setup code into init_chipset_hpt366(),
  112. * also fixing the interchanged 25/40 MHz PCI clock cases for HPT36x chips;
  113. * unify HPT36x/37x timing setup code and the speedproc handlers by joining
  114. * the register setting lists into the table indexed by the clock selected
  115. * - set the correct hwif->ultra_mask for each individual chip
  116. * - add Ultra and MW DMA mode filtering for the HPT37[24] based SATA cards
  117. * - stop resetting HPT370's state machine before each DMA transfer as that has
  118. * caused more harm than good
  119. * Sergei Shtylyov, <sshtylyov@ru.mvista.com> or <source@mvista.com>
  120. */
  121. #include <linux/types.h>
  122. #include <linux/module.h>
  123. #include <linux/kernel.h>
  124. #include <linux/delay.h>
  125. #include <linux/blkdev.h>
  126. #include <linux/interrupt.h>
  127. #include <linux/pci.h>
  128. #include <linux/init.h>
  129. #include <linux/ide.h>
  130. #include <linux/slab.h>
  131. #include <asm/uaccess.h>
  132. #include <asm/io.h>
  133. #define DRV_NAME "hpt366"
  134. /* various tuning parameters */
  135. #undef HPT_RESET_STATE_ENGINE
  136. #undef HPT_DELAY_INTERRUPT
  137. static const char *bad_ata100_5[] = {
  138. "IBM-DTLA-307075",
  139. "IBM-DTLA-307060",
  140. "IBM-DTLA-307045",
  141. "IBM-DTLA-307030",
  142. "IBM-DTLA-307020",
  143. "IBM-DTLA-307015",
  144. "IBM-DTLA-305040",
  145. "IBM-DTLA-305030",
  146. "IBM-DTLA-305020",
  147. "IC35L010AVER07-0",
  148. "IC35L020AVER07-0",
  149. "IC35L030AVER07-0",
  150. "IC35L040AVER07-0",
  151. "IC35L060AVER07-0",
  152. "WDC AC310200R",
  153. NULL
  154. };
  155. static const char *bad_ata66_4[] = {
  156. "IBM-DTLA-307075",
  157. "IBM-DTLA-307060",
  158. "IBM-DTLA-307045",
  159. "IBM-DTLA-307030",
  160. "IBM-DTLA-307020",
  161. "IBM-DTLA-307015",
  162. "IBM-DTLA-305040",
  163. "IBM-DTLA-305030",
  164. "IBM-DTLA-305020",
  165. "IC35L010AVER07-0",
  166. "IC35L020AVER07-0",
  167. "IC35L030AVER07-0",
  168. "IC35L040AVER07-0",
  169. "IC35L060AVER07-0",
  170. "WDC AC310200R",
  171. "MAXTOR STM3320620A",
  172. NULL
  173. };
  174. static const char *bad_ata66_3[] = {
  175. "WDC AC310200R",
  176. NULL
  177. };
  178. static const char *bad_ata33[] = {
  179. "Maxtor 92720U8", "Maxtor 92040U6", "Maxtor 91360U4", "Maxtor 91020U3", "Maxtor 90845U3", "Maxtor 90650U2",
  180. "Maxtor 91360D8", "Maxtor 91190D7", "Maxtor 91020D6", "Maxtor 90845D5", "Maxtor 90680D4", "Maxtor 90510D3", "Maxtor 90340D2",
  181. "Maxtor 91152D8", "Maxtor 91008D7", "Maxtor 90845D6", "Maxtor 90840D6", "Maxtor 90720D5", "Maxtor 90648D5", "Maxtor 90576D4",
  182. "Maxtor 90510D4",
  183. "Maxtor 90432D3", "Maxtor 90288D2", "Maxtor 90256D2",
  184. "Maxtor 91000D8", "Maxtor 90910D8", "Maxtor 90875D7", "Maxtor 90840D7", "Maxtor 90750D6", "Maxtor 90625D5", "Maxtor 90500D4",
  185. "Maxtor 91728D8", "Maxtor 91512D7", "Maxtor 91303D6", "Maxtor 91080D5", "Maxtor 90845D4", "Maxtor 90680D4", "Maxtor 90648D3", "Maxtor 90432D2",
  186. NULL
  187. };
  188. static u8 xfer_speeds[] = {
  189. XFER_UDMA_6,
  190. XFER_UDMA_5,
  191. XFER_UDMA_4,
  192. XFER_UDMA_3,
  193. XFER_UDMA_2,
  194. XFER_UDMA_1,
  195. XFER_UDMA_0,
  196. XFER_MW_DMA_2,
  197. XFER_MW_DMA_1,
  198. XFER_MW_DMA_0,
  199. XFER_PIO_4,
  200. XFER_PIO_3,
  201. XFER_PIO_2,
  202. XFER_PIO_1,
  203. XFER_PIO_0
  204. };
  205. /* Key for bus clock timings
  206. * 36x 37x
  207. * bits bits
  208. * 0:3 0:3 data_high_time. Inactive time of DIOW_/DIOR_ for PIO and MW DMA.
  209. * cycles = value + 1
  210. * 4:7 4:8 data_low_time. Active time of DIOW_/DIOR_ for PIO and MW DMA.
  211. * cycles = value + 1
  212. * 8:11 9:12 cmd_high_time. Inactive time of DIOW_/DIOR_ during task file
  213. * register access.
  214. * 12:15 13:17 cmd_low_time. Active time of DIOW_/DIOR_ during task file
  215. * register access.
  216. * 16:18 18:20 udma_cycle_time. Clock cycles for UDMA xfer.
  217. * - 21 CLK frequency: 0=ATA clock, 1=dual ATA clock.
  218. * 19:21 22:24 pre_high_time. Time to initialize the 1st cycle for PIO and
  219. * MW DMA xfer.
  220. * 22:24 25:27 cmd_pre_high_time. Time to initialize the 1st PIO cycle for
  221. * task file register access.
  222. * 28 28 UDMA enable.
  223. * 29 29 DMA enable.
  224. * 30 30 PIO MST enable. If set, the chip is in bus master mode during
  225. * PIO xfer.
  226. * 31 31 FIFO enable.
  227. */
  228. static u32 forty_base_hpt36x[] = {
  229. /* XFER_UDMA_6 */ 0x900fd943,
  230. /* XFER_UDMA_5 */ 0x900fd943,
  231. /* XFER_UDMA_4 */ 0x900fd943,
  232. /* XFER_UDMA_3 */ 0x900ad943,
  233. /* XFER_UDMA_2 */ 0x900bd943,
  234. /* XFER_UDMA_1 */ 0x9008d943,
  235. /* XFER_UDMA_0 */ 0x9008d943,
  236. /* XFER_MW_DMA_2 */ 0xa008d943,
  237. /* XFER_MW_DMA_1 */ 0xa010d955,
  238. /* XFER_MW_DMA_0 */ 0xa010d9fc,
  239. /* XFER_PIO_4 */ 0xc008d963,
  240. /* XFER_PIO_3 */ 0xc010d974,
  241. /* XFER_PIO_2 */ 0xc010d997,
  242. /* XFER_PIO_1 */ 0xc010d9c7,
  243. /* XFER_PIO_0 */ 0xc018d9d9
  244. };
  245. static u32 thirty_three_base_hpt36x[] = {
  246. /* XFER_UDMA_6 */ 0x90c9a731,
  247. /* XFER_UDMA_5 */ 0x90c9a731,
  248. /* XFER_UDMA_4 */ 0x90c9a731,
  249. /* XFER_UDMA_3 */ 0x90cfa731,
  250. /* XFER_UDMA_2 */ 0x90caa731,
  251. /* XFER_UDMA_1 */ 0x90cba731,
  252. /* XFER_UDMA_0 */ 0x90c8a731,
  253. /* XFER_MW_DMA_2 */ 0xa0c8a731,
  254. /* XFER_MW_DMA_1 */ 0xa0c8a732, /* 0xa0c8a733 */
  255. /* XFER_MW_DMA_0 */ 0xa0c8a797,
  256. /* XFER_PIO_4 */ 0xc0c8a731,
  257. /* XFER_PIO_3 */ 0xc0c8a742,
  258. /* XFER_PIO_2 */ 0xc0d0a753,
  259. /* XFER_PIO_1 */ 0xc0d0a7a3, /* 0xc0d0a793 */
  260. /* XFER_PIO_0 */ 0xc0d0a7aa /* 0xc0d0a7a7 */
  261. };
  262. static u32 twenty_five_base_hpt36x[] = {
  263. /* XFER_UDMA_6 */ 0x90c98521,
  264. /* XFER_UDMA_5 */ 0x90c98521,
  265. /* XFER_UDMA_4 */ 0x90c98521,
  266. /* XFER_UDMA_3 */ 0x90cf8521,
  267. /* XFER_UDMA_2 */ 0x90cf8521,
  268. /* XFER_UDMA_1 */ 0x90cb8521,
  269. /* XFER_UDMA_0 */ 0x90cb8521,
  270. /* XFER_MW_DMA_2 */ 0xa0ca8521,
  271. /* XFER_MW_DMA_1 */ 0xa0ca8532,
  272. /* XFER_MW_DMA_0 */ 0xa0ca8575,
  273. /* XFER_PIO_4 */ 0xc0ca8521,
  274. /* XFER_PIO_3 */ 0xc0ca8532,
  275. /* XFER_PIO_2 */ 0xc0ca8542,
  276. /* XFER_PIO_1 */ 0xc0d08572,
  277. /* XFER_PIO_0 */ 0xc0d08585
  278. };
  279. /*
  280. * The following are the new timing tables with PIO mode data/taskfile transfer
  281. * overclocking fixed...
  282. */
  283. /* This table is taken from the HPT370 data manual rev. 1.02 */
  284. static u32 thirty_three_base_hpt37x[] = {
  285. /* XFER_UDMA_6 */ 0x16455031, /* 0x16655031 ?? */
  286. /* XFER_UDMA_5 */ 0x16455031,
  287. /* XFER_UDMA_4 */ 0x16455031,
  288. /* XFER_UDMA_3 */ 0x166d5031,
  289. /* XFER_UDMA_2 */ 0x16495031,
  290. /* XFER_UDMA_1 */ 0x164d5033,
  291. /* XFER_UDMA_0 */ 0x16515097,
  292. /* XFER_MW_DMA_2 */ 0x26515031,
  293. /* XFER_MW_DMA_1 */ 0x26515033,
  294. /* XFER_MW_DMA_0 */ 0x26515097,
  295. /* XFER_PIO_4 */ 0x06515021,
  296. /* XFER_PIO_3 */ 0x06515022,
  297. /* XFER_PIO_2 */ 0x06515033,
  298. /* XFER_PIO_1 */ 0x06915065,
  299. /* XFER_PIO_0 */ 0x06d1508a
  300. };
  301. static u32 fifty_base_hpt37x[] = {
  302. /* XFER_UDMA_6 */ 0x1a861842,
  303. /* XFER_UDMA_5 */ 0x1a861842,
  304. /* XFER_UDMA_4 */ 0x1aae1842,
  305. /* XFER_UDMA_3 */ 0x1a8e1842,
  306. /* XFER_UDMA_2 */ 0x1a0e1842,
  307. /* XFER_UDMA_1 */ 0x1a161854,
  308. /* XFER_UDMA_0 */ 0x1a1a18ea,
  309. /* XFER_MW_DMA_2 */ 0x2a821842,
  310. /* XFER_MW_DMA_1 */ 0x2a821854,
  311. /* XFER_MW_DMA_0 */ 0x2a8218ea,
  312. /* XFER_PIO_4 */ 0x0a821842,
  313. /* XFER_PIO_3 */ 0x0a821843,
  314. /* XFER_PIO_2 */ 0x0a821855,
  315. /* XFER_PIO_1 */ 0x0ac218a8,
  316. /* XFER_PIO_0 */ 0x0b02190c
  317. };
  318. static u32 sixty_six_base_hpt37x[] = {
  319. /* XFER_UDMA_6 */ 0x1c86fe62,
  320. /* XFER_UDMA_5 */ 0x1caefe62, /* 0x1c8afe62 */
  321. /* XFER_UDMA_4 */ 0x1c8afe62,
  322. /* XFER_UDMA_3 */ 0x1c8efe62,
  323. /* XFER_UDMA_2 */ 0x1c92fe62,
  324. /* XFER_UDMA_1 */ 0x1c9afe62,
  325. /* XFER_UDMA_0 */ 0x1c82fe62,
  326. /* XFER_MW_DMA_2 */ 0x2c82fe62,
  327. /* XFER_MW_DMA_1 */ 0x2c82fe66,
  328. /* XFER_MW_DMA_0 */ 0x2c82ff2e,
  329. /* XFER_PIO_4 */ 0x0c82fe62,
  330. /* XFER_PIO_3 */ 0x0c82fe84,
  331. /* XFER_PIO_2 */ 0x0c82fea6,
  332. /* XFER_PIO_1 */ 0x0d02ff26,
  333. /* XFER_PIO_0 */ 0x0d42ff7f
  334. };
  335. #define HPT371_ALLOW_ATA133_6 1
  336. #define HPT302_ALLOW_ATA133_6 1
  337. #define HPT372_ALLOW_ATA133_6 1
  338. #define HPT370_ALLOW_ATA100_5 0
  339. #define HPT366_ALLOW_ATA66_4 1
  340. #define HPT366_ALLOW_ATA66_3 1
  341. /* Supported ATA clock frequencies */
  342. enum ata_clock {
  343. ATA_CLOCK_25MHZ,
  344. ATA_CLOCK_33MHZ,
  345. ATA_CLOCK_40MHZ,
  346. ATA_CLOCK_50MHZ,
  347. ATA_CLOCK_66MHZ,
  348. NUM_ATA_CLOCKS
  349. };
  350. struct hpt_timings {
  351. u32 pio_mask;
  352. u32 dma_mask;
  353. u32 ultra_mask;
  354. u32 *clock_table[NUM_ATA_CLOCKS];
  355. };
  356. /*
  357. * Hold all the HighPoint chip information in one place.
  358. */
  359. struct hpt_info {
  360. char *chip_name; /* Chip name */
  361. u8 chip_type; /* Chip type */
  362. u8 udma_mask; /* Allowed UltraDMA modes mask. */
  363. u8 dpll_clk; /* DPLL clock in MHz */
  364. u8 pci_clk; /* PCI clock in MHz */
  365. struct hpt_timings *timings; /* Chipset timing data */
  366. u8 clock; /* ATA clock selected */
  367. };
  368. /* Supported HighPoint chips */
  369. enum {
  370. HPT36x,
  371. HPT370,
  372. HPT370A,
  373. HPT374,
  374. HPT372,
  375. HPT372A,
  376. HPT302,
  377. HPT371,
  378. HPT372N,
  379. HPT302N,
  380. HPT371N
  381. };
  382. static struct hpt_timings hpt36x_timings = {
  383. .pio_mask = 0xc1f8ffff,
  384. .dma_mask = 0x303800ff,
  385. .ultra_mask = 0x30070000,
  386. .clock_table = {
  387. [ATA_CLOCK_25MHZ] = twenty_five_base_hpt36x,
  388. [ATA_CLOCK_33MHZ] = thirty_three_base_hpt36x,
  389. [ATA_CLOCK_40MHZ] = forty_base_hpt36x,
  390. [ATA_CLOCK_50MHZ] = NULL,
  391. [ATA_CLOCK_66MHZ] = NULL
  392. }
  393. };
  394. static struct hpt_timings hpt37x_timings = {
  395. .pio_mask = 0xcfc3ffff,
  396. .dma_mask = 0x31c001ff,
  397. .ultra_mask = 0x303c0000,
  398. .clock_table = {
  399. [ATA_CLOCK_25MHZ] = NULL,
  400. [ATA_CLOCK_33MHZ] = thirty_three_base_hpt37x,
  401. [ATA_CLOCK_40MHZ] = NULL,
  402. [ATA_CLOCK_50MHZ] = fifty_base_hpt37x,
  403. [ATA_CLOCK_66MHZ] = sixty_six_base_hpt37x
  404. }
  405. };
  406. static const struct hpt_info hpt36x = {
  407. .chip_name = "HPT36x",
  408. .chip_type = HPT36x,
  409. .udma_mask = HPT366_ALLOW_ATA66_3 ? (HPT366_ALLOW_ATA66_4 ? ATA_UDMA4 : ATA_UDMA3) : ATA_UDMA2,
  410. .dpll_clk = 0, /* no DPLL */
  411. .timings = &hpt36x_timings
  412. };
  413. static const struct hpt_info hpt370 = {
  414. .chip_name = "HPT370",
  415. .chip_type = HPT370,
  416. .udma_mask = HPT370_ALLOW_ATA100_5 ? ATA_UDMA5 : ATA_UDMA4,
  417. .dpll_clk = 48,
  418. .timings = &hpt37x_timings
  419. };
  420. static const struct hpt_info hpt370a = {
  421. .chip_name = "HPT370A",
  422. .chip_type = HPT370A,
  423. .udma_mask = HPT370_ALLOW_ATA100_5 ? ATA_UDMA5 : ATA_UDMA4,
  424. .dpll_clk = 48,
  425. .timings = &hpt37x_timings
  426. };
  427. static const struct hpt_info hpt374 = {
  428. .chip_name = "HPT374",
  429. .chip_type = HPT374,
  430. .udma_mask = ATA_UDMA5,
  431. .dpll_clk = 48,
  432. .timings = &hpt37x_timings
  433. };
  434. static const struct hpt_info hpt372 = {
  435. .chip_name = "HPT372",
  436. .chip_type = HPT372,
  437. .udma_mask = HPT372_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5,
  438. .dpll_clk = 55,
  439. .timings = &hpt37x_timings
  440. };
  441. static const struct hpt_info hpt372a = {
  442. .chip_name = "HPT372A",
  443. .chip_type = HPT372A,
  444. .udma_mask = HPT372_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5,
  445. .dpll_clk = 66,
  446. .timings = &hpt37x_timings
  447. };
  448. static const struct hpt_info hpt302 = {
  449. .chip_name = "HPT302",
  450. .chip_type = HPT302,
  451. .udma_mask = HPT302_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5,
  452. .dpll_clk = 66,
  453. .timings = &hpt37x_timings
  454. };
  455. static const struct hpt_info hpt371 = {
  456. .chip_name = "HPT371",
  457. .chip_type = HPT371,
  458. .udma_mask = HPT371_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5,
  459. .dpll_clk = 66,
  460. .timings = &hpt37x_timings
  461. };
  462. static const struct hpt_info hpt372n = {
  463. .chip_name = "HPT372N",
  464. .chip_type = HPT372N,
  465. .udma_mask = HPT372_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5,
  466. .dpll_clk = 77,
  467. .timings = &hpt37x_timings
  468. };
  469. static const struct hpt_info hpt302n = {
  470. .chip_name = "HPT302N",
  471. .chip_type = HPT302N,
  472. .udma_mask = HPT302_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5,
  473. .dpll_clk = 77,
  474. .timings = &hpt37x_timings
  475. };
  476. static const struct hpt_info hpt371n = {
  477. .chip_name = "HPT371N",
  478. .chip_type = HPT371N,
  479. .udma_mask = HPT371_ALLOW_ATA133_6 ? ATA_UDMA6 : ATA_UDMA5,
  480. .dpll_clk = 77,
  481. .timings = &hpt37x_timings
  482. };
  483. static int check_in_drive_list(ide_drive_t *drive, const char **list)
  484. {
  485. char *m = (char *)&drive->id[ATA_ID_PROD];
  486. while (*list)
  487. if (!strcmp(*list++, m))
  488. return 1;
  489. return 0;
  490. }
  491. static struct hpt_info *hpt3xx_get_info(struct device *dev)
  492. {
  493. struct ide_host *host = dev_get_drvdata(dev);
  494. struct hpt_info *info = (struct hpt_info *)host->host_priv;
  495. return dev == host->dev[1] ? info + 1 : info;
  496. }
  497. /*
  498. * The Marvell bridge chips used on the HighPoint SATA cards do not seem
  499. * to support the UltraDMA modes 1, 2, and 3 as well as any MWDMA modes...
  500. */
  501. static u8 hpt3xx_udma_filter(ide_drive_t *drive)
  502. {
  503. ide_hwif_t *hwif = drive->hwif;
  504. struct hpt_info *info = hpt3xx_get_info(hwif->dev);
  505. u8 mask = hwif->ultra_mask;
  506. switch (info->chip_type) {
  507. case HPT36x:
  508. if (!HPT366_ALLOW_ATA66_4 ||
  509. check_in_drive_list(drive, bad_ata66_4))
  510. mask = ATA_UDMA3;
  511. if (!HPT366_ALLOW_ATA66_3 ||
  512. check_in_drive_list(drive, bad_ata66_3))
  513. mask = ATA_UDMA2;
  514. break;
  515. case HPT370:
  516. if (!HPT370_ALLOW_ATA100_5 ||
  517. check_in_drive_list(drive, bad_ata100_5))
  518. mask = ATA_UDMA4;
  519. break;
  520. case HPT370A:
  521. if (!HPT370_ALLOW_ATA100_5 ||
  522. check_in_drive_list(drive, bad_ata100_5))
  523. return ATA_UDMA4;
  524. case HPT372 :
  525. case HPT372A:
  526. case HPT372N:
  527. case HPT374 :
  528. if (ata_id_is_sata(drive->id))
  529. mask &= ~0x0e;
  530. /* Fall thru */
  531. default:
  532. return mask;
  533. }
  534. return check_in_drive_list(drive, bad_ata33) ? 0x00 : mask;
  535. }
  536. static u8 hpt3xx_mdma_filter(ide_drive_t *drive)
  537. {
  538. ide_hwif_t *hwif = drive->hwif;
  539. struct hpt_info *info = hpt3xx_get_info(hwif->dev);
  540. switch (info->chip_type) {
  541. case HPT372 :
  542. case HPT372A:
  543. case HPT372N:
  544. case HPT374 :
  545. if (ata_id_is_sata(drive->id))
  546. return 0x00;
  547. /* Fall thru */
  548. default:
  549. return 0x07;
  550. }
  551. }
  552. static u32 get_speed_setting(u8 speed, struct hpt_info *info)
  553. {
  554. int i;
  555. /*
  556. * Lookup the transfer mode table to get the index into
  557. * the timing table.
  558. *
  559. * NOTE: For XFER_PIO_SLOW, PIO mode 0 timings will be used.
  560. */
  561. for (i = 0; i < ARRAY_SIZE(xfer_speeds) - 1; i++)
  562. if (xfer_speeds[i] == speed)
  563. break;
  564. return info->timings->clock_table[info->clock][i];
  565. }
  566. static void hpt3xx_set_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  567. {
  568. struct pci_dev *dev = to_pci_dev(hwif->dev);
  569. struct hpt_info *info = hpt3xx_get_info(hwif->dev);
  570. struct hpt_timings *t = info->timings;
  571. u8 itr_addr = 0x40 + (drive->dn * 4);
  572. u32 old_itr = 0;
  573. const u8 speed = drive->dma_mode;
  574. u32 new_itr = get_speed_setting(speed, info);
  575. u32 itr_mask = speed < XFER_MW_DMA_0 ? t->pio_mask :
  576. (speed < XFER_UDMA_0 ? t->dma_mask :
  577. t->ultra_mask);
  578. pci_read_config_dword(dev, itr_addr, &old_itr);
  579. new_itr = (old_itr & ~itr_mask) | (new_itr & itr_mask);
  580. /*
  581. * Disable on-chip PIO FIFO/buffer (and PIO MST mode as well)
  582. * to avoid problems handling I/O errors later
  583. */
  584. new_itr &= ~0xc0000000;
  585. pci_write_config_dword(dev, itr_addr, new_itr);
  586. }
  587. static void hpt3xx_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  588. {
  589. drive->dma_mode = drive->pio_mode;
  590. hpt3xx_set_mode(hwif, drive);
  591. }
  592. static void hpt3xx_maskproc(ide_drive_t *drive, int mask)
  593. {
  594. ide_hwif_t *hwif = drive->hwif;
  595. struct pci_dev *dev = to_pci_dev(hwif->dev);
  596. struct hpt_info *info = hpt3xx_get_info(hwif->dev);
  597. if ((drive->dev_flags & IDE_DFLAG_NIEN_QUIRK) == 0)
  598. return;
  599. if (info->chip_type >= HPT370) {
  600. u8 scr1 = 0;
  601. pci_read_config_byte(dev, 0x5a, &scr1);
  602. if (((scr1 & 0x10) >> 4) != mask) {
  603. if (mask)
  604. scr1 |= 0x10;
  605. else
  606. scr1 &= ~0x10;
  607. pci_write_config_byte(dev, 0x5a, scr1);
  608. }
  609. } else if (mask)
  610. disable_irq(hwif->irq);
  611. else
  612. enable_irq(hwif->irq);
  613. }
  614. /*
  615. * This is specific to the HPT366 UDMA chipset
  616. * by HighPoint|Triones Technologies, Inc.
  617. */
  618. static void hpt366_dma_lost_irq(ide_drive_t *drive)
  619. {
  620. struct pci_dev *dev = to_pci_dev(drive->hwif->dev);
  621. u8 mcr1 = 0, mcr3 = 0, scr1 = 0;
  622. pci_read_config_byte(dev, 0x50, &mcr1);
  623. pci_read_config_byte(dev, 0x52, &mcr3);
  624. pci_read_config_byte(dev, 0x5a, &scr1);
  625. printk("%s: (%s) mcr1=0x%02x, mcr3=0x%02x, scr1=0x%02x\n",
  626. drive->name, __func__, mcr1, mcr3, scr1);
  627. if (scr1 & 0x10)
  628. pci_write_config_byte(dev, 0x5a, scr1 & ~0x10);
  629. ide_dma_lost_irq(drive);
  630. }
  631. static void hpt370_clear_engine(ide_drive_t *drive)
  632. {
  633. ide_hwif_t *hwif = drive->hwif;
  634. struct pci_dev *dev = to_pci_dev(hwif->dev);
  635. pci_write_config_byte(dev, hwif->select_data, 0x37);
  636. udelay(10);
  637. }
  638. static void hpt370_irq_timeout(ide_drive_t *drive)
  639. {
  640. ide_hwif_t *hwif = drive->hwif;
  641. struct pci_dev *dev = to_pci_dev(hwif->dev);
  642. u16 bfifo = 0;
  643. u8 dma_cmd;
  644. pci_read_config_word(dev, hwif->select_data + 2, &bfifo);
  645. printk(KERN_DEBUG "%s: %d bytes in FIFO\n", drive->name, bfifo & 0x1ff);
  646. /* get DMA command mode */
  647. dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
  648. /* stop DMA */
  649. outb(dma_cmd & ~ATA_DMA_START, hwif->dma_base + ATA_DMA_CMD);
  650. hpt370_clear_engine(drive);
  651. }
  652. static void hpt370_dma_start(ide_drive_t *drive)
  653. {
  654. #ifdef HPT_RESET_STATE_ENGINE
  655. hpt370_clear_engine(drive);
  656. #endif
  657. ide_dma_start(drive);
  658. }
  659. static int hpt370_dma_end(ide_drive_t *drive)
  660. {
  661. ide_hwif_t *hwif = drive->hwif;
  662. u8 dma_stat = inb(hwif->dma_base + ATA_DMA_STATUS);
  663. if (dma_stat & ATA_DMA_ACTIVE) {
  664. /* wait a little */
  665. udelay(20);
  666. dma_stat = inb(hwif->dma_base + ATA_DMA_STATUS);
  667. if (dma_stat & ATA_DMA_ACTIVE)
  668. hpt370_irq_timeout(drive);
  669. }
  670. return ide_dma_end(drive);
  671. }
  672. /* returns 1 if DMA IRQ issued, 0 otherwise */
  673. static int hpt374_dma_test_irq(ide_drive_t *drive)
  674. {
  675. ide_hwif_t *hwif = drive->hwif;
  676. struct pci_dev *dev = to_pci_dev(hwif->dev);
  677. u16 bfifo = 0;
  678. u8 dma_stat;
  679. pci_read_config_word(dev, hwif->select_data + 2, &bfifo);
  680. if (bfifo & 0x1FF) {
  681. // printk("%s: %d bytes in FIFO\n", drive->name, bfifo);
  682. return 0;
  683. }
  684. dma_stat = inb(hwif->dma_base + ATA_DMA_STATUS);
  685. /* return 1 if INTR asserted */
  686. if (dma_stat & ATA_DMA_INTR)
  687. return 1;
  688. return 0;
  689. }
  690. static int hpt374_dma_end(ide_drive_t *drive)
  691. {
  692. ide_hwif_t *hwif = drive->hwif;
  693. struct pci_dev *dev = to_pci_dev(hwif->dev);
  694. u8 mcr = 0, mcr_addr = hwif->select_data;
  695. u8 bwsr = 0, mask = hwif->channel ? 0x02 : 0x01;
  696. pci_read_config_byte(dev, 0x6a, &bwsr);
  697. pci_read_config_byte(dev, mcr_addr, &mcr);
  698. if (bwsr & mask)
  699. pci_write_config_byte(dev, mcr_addr, mcr | 0x30);
  700. return ide_dma_end(drive);
  701. }
  702. /**
  703. * hpt3xxn_set_clock - perform clock switching dance
  704. * @hwif: hwif to switch
  705. * @mode: clocking mode (0x21 for write, 0x23 otherwise)
  706. *
  707. * Switch the DPLL clock on the HPT3xxN devices. This is a right mess.
  708. */
  709. static void hpt3xxn_set_clock(ide_hwif_t *hwif, u8 mode)
  710. {
  711. unsigned long base = hwif->extra_base;
  712. u8 scr2 = inb(base + 0x6b);
  713. if ((scr2 & 0x7f) == mode)
  714. return;
  715. /* Tristate the bus */
  716. outb(0x80, base + 0x63);
  717. outb(0x80, base + 0x67);
  718. /* Switch clock and reset channels */
  719. outb(mode, base + 0x6b);
  720. outb(0xc0, base + 0x69);
  721. /*
  722. * Reset the state machines.
  723. * NOTE: avoid accidentally enabling the disabled channels.
  724. */
  725. outb(inb(base + 0x60) | 0x32, base + 0x60);
  726. outb(inb(base + 0x64) | 0x32, base + 0x64);
  727. /* Complete reset */
  728. outb(0x00, base + 0x69);
  729. /* Reconnect channels to bus */
  730. outb(0x00, base + 0x63);
  731. outb(0x00, base + 0x67);
  732. }
  733. /**
  734. * hpt3xxn_rw_disk - prepare for I/O
  735. * @drive: drive for command
  736. * @rq: block request structure
  737. *
  738. * This is called when a disk I/O is issued to HPT3xxN.
  739. * We need it because of the clock switching.
  740. */
  741. static void hpt3xxn_rw_disk(ide_drive_t *drive, struct request *rq)
  742. {
  743. hpt3xxn_set_clock(drive->hwif, rq_data_dir(rq) ? 0x21 : 0x23);
  744. }
  745. /**
  746. * hpt37x_calibrate_dpll - calibrate the DPLL
  747. * @dev: PCI device
  748. *
  749. * Perform a calibration cycle on the DPLL.
  750. * Returns 1 if this succeeds
  751. */
  752. static int hpt37x_calibrate_dpll(struct pci_dev *dev, u16 f_low, u16 f_high)
  753. {
  754. u32 dpll = (f_high << 16) | f_low | 0x100;
  755. u8 scr2;
  756. int i;
  757. pci_write_config_dword(dev, 0x5c, dpll);
  758. /* Wait for oscillator ready */
  759. for(i = 0; i < 0x5000; ++i) {
  760. udelay(50);
  761. pci_read_config_byte(dev, 0x5b, &scr2);
  762. if (scr2 & 0x80)
  763. break;
  764. }
  765. /* See if it stays ready (we'll just bail out if it's not yet) */
  766. for(i = 0; i < 0x1000; ++i) {
  767. pci_read_config_byte(dev, 0x5b, &scr2);
  768. /* DPLL destabilized? */
  769. if(!(scr2 & 0x80))
  770. return 0;
  771. }
  772. /* Turn off tuning, we have the DPLL set */
  773. pci_read_config_dword (dev, 0x5c, &dpll);
  774. pci_write_config_dword(dev, 0x5c, (dpll & ~0x100));
  775. return 1;
  776. }
  777. static void hpt3xx_disable_fast_irq(struct pci_dev *dev, u8 mcr_addr)
  778. {
  779. struct ide_host *host = pci_get_drvdata(dev);
  780. struct hpt_info *info = host->host_priv + (&dev->dev == host->dev[1]);
  781. u8 chip_type = info->chip_type;
  782. u8 new_mcr, old_mcr = 0;
  783. /*
  784. * Disable the "fast interrupt" prediction. Don't hold off
  785. * on interrupts. (== 0x01 despite what the docs say)
  786. */
  787. pci_read_config_byte(dev, mcr_addr + 1, &old_mcr);
  788. if (chip_type >= HPT374)
  789. new_mcr = old_mcr & ~0x07;
  790. else if (chip_type >= HPT370) {
  791. new_mcr = old_mcr;
  792. new_mcr &= ~0x02;
  793. #ifdef HPT_DELAY_INTERRUPT
  794. new_mcr &= ~0x01;
  795. #else
  796. new_mcr |= 0x01;
  797. #endif
  798. } else /* HPT366 and HPT368 */
  799. new_mcr = old_mcr & ~0x80;
  800. if (new_mcr != old_mcr)
  801. pci_write_config_byte(dev, mcr_addr + 1, new_mcr);
  802. }
  803. static int init_chipset_hpt366(struct pci_dev *dev)
  804. {
  805. unsigned long io_base = pci_resource_start(dev, 4);
  806. struct hpt_info *info = hpt3xx_get_info(&dev->dev);
  807. const char *name = DRV_NAME;
  808. u8 pci_clk, dpll_clk = 0; /* PCI and DPLL clock in MHz */
  809. u8 chip_type;
  810. enum ata_clock clock;
  811. chip_type = info->chip_type;
  812. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (L1_CACHE_BYTES / 4));
  813. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x78);
  814. pci_write_config_byte(dev, PCI_MIN_GNT, 0x08);
  815. pci_write_config_byte(dev, PCI_MAX_LAT, 0x08);
  816. /*
  817. * First, try to estimate the PCI clock frequency...
  818. */
  819. if (chip_type >= HPT370) {
  820. u8 scr1 = 0;
  821. u16 f_cnt = 0;
  822. u32 temp = 0;
  823. /* Interrupt force enable. */
  824. pci_read_config_byte(dev, 0x5a, &scr1);
  825. if (scr1 & 0x10)
  826. pci_write_config_byte(dev, 0x5a, scr1 & ~0x10);
  827. /*
  828. * HighPoint does this for HPT372A.
  829. * NOTE: This register is only writeable via I/O space.
  830. */
  831. if (chip_type == HPT372A)
  832. outb(0x0e, io_base + 0x9c);
  833. /*
  834. * Default to PCI clock. Make sure MA15/16 are set to output
  835. * to prevent drives having problems with 40-pin cables.
  836. */
  837. pci_write_config_byte(dev, 0x5b, 0x23);
  838. /*
  839. * We'll have to read f_CNT value in order to determine
  840. * the PCI clock frequency according to the following ratio:
  841. *
  842. * f_CNT = Fpci * 192 / Fdpll
  843. *
  844. * First try reading the register in which the HighPoint BIOS
  845. * saves f_CNT value before reprogramming the DPLL from its
  846. * default setting (which differs for the various chips).
  847. *
  848. * NOTE: This register is only accessible via I/O space;
  849. * HPT374 BIOS only saves it for the function 0, so we have to
  850. * always read it from there -- no need to check the result of
  851. * pci_get_slot() for the function 0 as the whole device has
  852. * been already "pinned" (via function 1) in init_setup_hpt374()
  853. */
  854. if (chip_type == HPT374 && (PCI_FUNC(dev->devfn) & 1)) {
  855. struct pci_dev *dev1 = pci_get_slot(dev->bus,
  856. dev->devfn - 1);
  857. unsigned long io_base = pci_resource_start(dev1, 4);
  858. temp = inl(io_base + 0x90);
  859. pci_dev_put(dev1);
  860. } else
  861. temp = inl(io_base + 0x90);
  862. /*
  863. * In case the signature check fails, we'll have to
  864. * resort to reading the f_CNT register itself in hopes
  865. * that nobody has touched the DPLL yet...
  866. */
  867. if ((temp & 0xFFFFF000) != 0xABCDE000) {
  868. int i;
  869. printk(KERN_WARNING "%s %s: no clock data saved by "
  870. "BIOS\n", name, pci_name(dev));
  871. /* Calculate the average value of f_CNT. */
  872. for (temp = i = 0; i < 128; i++) {
  873. pci_read_config_word(dev, 0x78, &f_cnt);
  874. temp += f_cnt & 0x1ff;
  875. mdelay(1);
  876. }
  877. f_cnt = temp / 128;
  878. } else
  879. f_cnt = temp & 0x1ff;
  880. dpll_clk = info->dpll_clk;
  881. pci_clk = (f_cnt * dpll_clk) / 192;
  882. /* Clamp PCI clock to bands. */
  883. if (pci_clk < 40)
  884. pci_clk = 33;
  885. else if(pci_clk < 45)
  886. pci_clk = 40;
  887. else if(pci_clk < 55)
  888. pci_clk = 50;
  889. else
  890. pci_clk = 66;
  891. printk(KERN_INFO "%s %s: DPLL base: %d MHz, f_CNT: %d, "
  892. "assuming %d MHz PCI\n", name, pci_name(dev),
  893. dpll_clk, f_cnt, pci_clk);
  894. } else {
  895. u32 itr1 = 0;
  896. pci_read_config_dword(dev, 0x40, &itr1);
  897. /* Detect PCI clock by looking at cmd_high_time. */
  898. switch((itr1 >> 8) & 0x07) {
  899. case 0x09:
  900. pci_clk = 40;
  901. break;
  902. case 0x05:
  903. pci_clk = 25;
  904. break;
  905. case 0x07:
  906. default:
  907. pci_clk = 33;
  908. break;
  909. }
  910. }
  911. /* Let's assume we'll use PCI clock for the ATA clock... */
  912. switch (pci_clk) {
  913. case 25:
  914. clock = ATA_CLOCK_25MHZ;
  915. break;
  916. case 33:
  917. default:
  918. clock = ATA_CLOCK_33MHZ;
  919. break;
  920. case 40:
  921. clock = ATA_CLOCK_40MHZ;
  922. break;
  923. case 50:
  924. clock = ATA_CLOCK_50MHZ;
  925. break;
  926. case 66:
  927. clock = ATA_CLOCK_66MHZ;
  928. break;
  929. }
  930. /*
  931. * Only try the DPLL if we don't have a table for the PCI clock that
  932. * we are running at for HPT370/A, always use it for anything newer...
  933. *
  934. * NOTE: Using the internal DPLL results in slow reads on 33 MHz PCI.
  935. * We also don't like using the DPLL because this causes glitches
  936. * on PRST-/SRST- when the state engine gets reset...
  937. */
  938. if (chip_type >= HPT374 || info->timings->clock_table[clock] == NULL) {
  939. u16 f_low, delta = pci_clk < 50 ? 2 : 4;
  940. int adjust;
  941. /*
  942. * Select 66 MHz DPLL clock only if UltraATA/133 mode is
  943. * supported/enabled, use 50 MHz DPLL clock otherwise...
  944. */
  945. if (info->udma_mask == ATA_UDMA6) {
  946. dpll_clk = 66;
  947. clock = ATA_CLOCK_66MHZ;
  948. } else if (dpll_clk) { /* HPT36x chips don't have DPLL */
  949. dpll_clk = 50;
  950. clock = ATA_CLOCK_50MHZ;
  951. }
  952. if (info->timings->clock_table[clock] == NULL) {
  953. printk(KERN_ERR "%s %s: unknown bus timing!\n",
  954. name, pci_name(dev));
  955. return -EIO;
  956. }
  957. /* Select the DPLL clock. */
  958. pci_write_config_byte(dev, 0x5b, 0x21);
  959. /*
  960. * Adjust the DPLL based upon PCI clock, enable it,
  961. * and wait for stabilization...
  962. */
  963. f_low = (pci_clk * 48) / dpll_clk;
  964. for (adjust = 0; adjust < 8; adjust++) {
  965. if(hpt37x_calibrate_dpll(dev, f_low, f_low + delta))
  966. break;
  967. /*
  968. * See if it'll settle at a fractionally different clock
  969. */
  970. if (adjust & 1)
  971. f_low -= adjust >> 1;
  972. else
  973. f_low += adjust >> 1;
  974. }
  975. if (adjust == 8) {
  976. printk(KERN_ERR "%s %s: DPLL did not stabilize!\n",
  977. name, pci_name(dev));
  978. return -EIO;
  979. }
  980. printk(KERN_INFO "%s %s: using %d MHz DPLL clock\n",
  981. name, pci_name(dev), dpll_clk);
  982. } else {
  983. /* Mark the fact that we're not using the DPLL. */
  984. dpll_clk = 0;
  985. printk(KERN_INFO "%s %s: using %d MHz PCI clock\n",
  986. name, pci_name(dev), pci_clk);
  987. }
  988. /* Store the clock frequencies. */
  989. info->dpll_clk = dpll_clk;
  990. info->pci_clk = pci_clk;
  991. info->clock = clock;
  992. if (chip_type >= HPT370) {
  993. u8 mcr1, mcr4;
  994. /*
  995. * Reset the state engines.
  996. * NOTE: Avoid accidentally enabling the disabled channels.
  997. */
  998. pci_read_config_byte (dev, 0x50, &mcr1);
  999. pci_read_config_byte (dev, 0x54, &mcr4);
  1000. pci_write_config_byte(dev, 0x50, (mcr1 | 0x32));
  1001. pci_write_config_byte(dev, 0x54, (mcr4 | 0x32));
  1002. udelay(100);
  1003. }
  1004. /*
  1005. * On HPT371N, if ATA clock is 66 MHz we must set bit 2 in
  1006. * the MISC. register to stretch the UltraDMA Tss timing.
  1007. * NOTE: This register is only writeable via I/O space.
  1008. */
  1009. if (chip_type == HPT371N && clock == ATA_CLOCK_66MHZ)
  1010. outb(inb(io_base + 0x9c) | 0x04, io_base + 0x9c);
  1011. hpt3xx_disable_fast_irq(dev, 0x50);
  1012. hpt3xx_disable_fast_irq(dev, 0x54);
  1013. return 0;
  1014. }
  1015. static u8 hpt3xx_cable_detect(ide_hwif_t *hwif)
  1016. {
  1017. struct pci_dev *dev = to_pci_dev(hwif->dev);
  1018. struct hpt_info *info = hpt3xx_get_info(hwif->dev);
  1019. u8 chip_type = info->chip_type;
  1020. u8 scr1 = 0, ata66 = hwif->channel ? 0x01 : 0x02;
  1021. /*
  1022. * The HPT37x uses the CBLID pins as outputs for MA15/MA16
  1023. * address lines to access an external EEPROM. To read valid
  1024. * cable detect state the pins must be enabled as inputs.
  1025. */
  1026. if (chip_type == HPT374 && (PCI_FUNC(dev->devfn) & 1)) {
  1027. /*
  1028. * HPT374 PCI function 1
  1029. * - set bit 15 of reg 0x52 to enable TCBLID as input
  1030. * - set bit 15 of reg 0x56 to enable FCBLID as input
  1031. */
  1032. u8 mcr_addr = hwif->select_data + 2;
  1033. u16 mcr;
  1034. pci_read_config_word(dev, mcr_addr, &mcr);
  1035. pci_write_config_word(dev, mcr_addr, mcr | 0x8000);
  1036. /* Debounce, then read cable ID register */
  1037. udelay(10);
  1038. pci_read_config_byte(dev, 0x5a, &scr1);
  1039. pci_write_config_word(dev, mcr_addr, mcr);
  1040. } else if (chip_type >= HPT370) {
  1041. /*
  1042. * HPT370/372 and 374 pcifn 0
  1043. * - clear bit 0 of reg 0x5b to enable P/SCBLID as inputs
  1044. */
  1045. u8 scr2 = 0;
  1046. pci_read_config_byte(dev, 0x5b, &scr2);
  1047. pci_write_config_byte(dev, 0x5b, scr2 & ~1);
  1048. /* Debounce, then read cable ID register */
  1049. udelay(10);
  1050. pci_read_config_byte(dev, 0x5a, &scr1);
  1051. pci_write_config_byte(dev, 0x5b, scr2);
  1052. } else
  1053. pci_read_config_byte(dev, 0x5a, &scr1);
  1054. return (scr1 & ata66) ? ATA_CBL_PATA40 : ATA_CBL_PATA80;
  1055. }
  1056. static void init_hwif_hpt366(ide_hwif_t *hwif)
  1057. {
  1058. struct hpt_info *info = hpt3xx_get_info(hwif->dev);
  1059. u8 chip_type = info->chip_type;
  1060. /* Cache the channel's MISC. control registers' offset */
  1061. hwif->select_data = hwif->channel ? 0x54 : 0x50;
  1062. /*
  1063. * HPT3xxN chips have some complications:
  1064. *
  1065. * - on 33 MHz PCI we must clock switch
  1066. * - on 66 MHz PCI we must NOT use the PCI clock
  1067. */
  1068. if (chip_type >= HPT372N && info->dpll_clk && info->pci_clk < 66) {
  1069. /*
  1070. * Clock is shared between the channels,
  1071. * so we'll have to serialize them... :-(
  1072. */
  1073. hwif->host->host_flags |= IDE_HFLAG_SERIALIZE;
  1074. hwif->rw_disk = &hpt3xxn_rw_disk;
  1075. }
  1076. }
  1077. static int init_dma_hpt366(ide_hwif_t *hwif,
  1078. const struct ide_port_info *d)
  1079. {
  1080. struct pci_dev *dev = to_pci_dev(hwif->dev);
  1081. unsigned long flags, base = ide_pci_dma_base(hwif, d);
  1082. u8 dma_old, dma_new, masterdma = 0, slavedma = 0;
  1083. if (base == 0)
  1084. return -1;
  1085. hwif->dma_base = base;
  1086. if (ide_pci_check_simplex(hwif, d) < 0)
  1087. return -1;
  1088. if (ide_pci_set_master(dev, d->name) < 0)
  1089. return -1;
  1090. dma_old = inb(base + 2);
  1091. local_irq_save(flags);
  1092. dma_new = dma_old;
  1093. pci_read_config_byte(dev, hwif->channel ? 0x4b : 0x43, &masterdma);
  1094. pci_read_config_byte(dev, hwif->channel ? 0x4f : 0x47, &slavedma);
  1095. if (masterdma & 0x30) dma_new |= 0x20;
  1096. if ( slavedma & 0x30) dma_new |= 0x40;
  1097. if (dma_new != dma_old)
  1098. outb(dma_new, base + 2);
  1099. local_irq_restore(flags);
  1100. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx\n",
  1101. hwif->name, base, base + 7);
  1102. hwif->extra_base = base + (hwif->channel ? 8 : 16);
  1103. if (ide_allocate_dma_engine(hwif))
  1104. return -1;
  1105. return 0;
  1106. }
  1107. static void hpt374_init(struct pci_dev *dev, struct pci_dev *dev2)
  1108. {
  1109. if (dev2->irq != dev->irq) {
  1110. /* FIXME: we need a core pci_set_interrupt() */
  1111. dev2->irq = dev->irq;
  1112. printk(KERN_INFO DRV_NAME " %s: PCI config space interrupt "
  1113. "fixed\n", pci_name(dev2));
  1114. }
  1115. }
  1116. static void hpt371_init(struct pci_dev *dev)
  1117. {
  1118. u8 mcr1 = 0;
  1119. /*
  1120. * HPT371 chips physically have only one channel, the secondary one,
  1121. * but the primary channel registers do exist! Go figure...
  1122. * So, we manually disable the non-existing channel here
  1123. * (if the BIOS hasn't done this already).
  1124. */
  1125. pci_read_config_byte(dev, 0x50, &mcr1);
  1126. if (mcr1 & 0x04)
  1127. pci_write_config_byte(dev, 0x50, mcr1 & ~0x04);
  1128. }
  1129. static int hpt36x_init(struct pci_dev *dev, struct pci_dev *dev2)
  1130. {
  1131. u8 mcr1 = 0, pin1 = 0, pin2 = 0;
  1132. /*
  1133. * Now we'll have to force both channels enabled if
  1134. * at least one of them has been enabled by BIOS...
  1135. */
  1136. pci_read_config_byte(dev, 0x50, &mcr1);
  1137. if (mcr1 & 0x30)
  1138. pci_write_config_byte(dev, 0x50, mcr1 | 0x30);
  1139. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin1);
  1140. pci_read_config_byte(dev2, PCI_INTERRUPT_PIN, &pin2);
  1141. if (pin1 != pin2 && dev->irq == dev2->irq) {
  1142. printk(KERN_INFO DRV_NAME " %s: onboard version of chipset, "
  1143. "pin1=%d pin2=%d\n", pci_name(dev), pin1, pin2);
  1144. return 1;
  1145. }
  1146. return 0;
  1147. }
  1148. #define IDE_HFLAGS_HPT3XX \
  1149. (IDE_HFLAG_NO_ATAPI_DMA | \
  1150. IDE_HFLAG_OFF_BOARD)
  1151. static const struct ide_port_ops hpt3xx_port_ops = {
  1152. .set_pio_mode = hpt3xx_set_pio_mode,
  1153. .set_dma_mode = hpt3xx_set_mode,
  1154. .maskproc = hpt3xx_maskproc,
  1155. .mdma_filter = hpt3xx_mdma_filter,
  1156. .udma_filter = hpt3xx_udma_filter,
  1157. .cable_detect = hpt3xx_cable_detect,
  1158. };
  1159. static const struct ide_dma_ops hpt37x_dma_ops = {
  1160. .dma_host_set = ide_dma_host_set,
  1161. .dma_setup = ide_dma_setup,
  1162. .dma_start = ide_dma_start,
  1163. .dma_end = hpt374_dma_end,
  1164. .dma_test_irq = hpt374_dma_test_irq,
  1165. .dma_lost_irq = ide_dma_lost_irq,
  1166. .dma_timer_expiry = ide_dma_sff_timer_expiry,
  1167. .dma_sff_read_status = ide_dma_sff_read_status,
  1168. };
  1169. static const struct ide_dma_ops hpt370_dma_ops = {
  1170. .dma_host_set = ide_dma_host_set,
  1171. .dma_setup = ide_dma_setup,
  1172. .dma_start = hpt370_dma_start,
  1173. .dma_end = hpt370_dma_end,
  1174. .dma_test_irq = ide_dma_test_irq,
  1175. .dma_lost_irq = ide_dma_lost_irq,
  1176. .dma_timer_expiry = ide_dma_sff_timer_expiry,
  1177. .dma_clear = hpt370_irq_timeout,
  1178. .dma_sff_read_status = ide_dma_sff_read_status,
  1179. };
  1180. static const struct ide_dma_ops hpt36x_dma_ops = {
  1181. .dma_host_set = ide_dma_host_set,
  1182. .dma_setup = ide_dma_setup,
  1183. .dma_start = ide_dma_start,
  1184. .dma_end = ide_dma_end,
  1185. .dma_test_irq = ide_dma_test_irq,
  1186. .dma_lost_irq = hpt366_dma_lost_irq,
  1187. .dma_timer_expiry = ide_dma_sff_timer_expiry,
  1188. .dma_sff_read_status = ide_dma_sff_read_status,
  1189. };
  1190. static const struct ide_port_info hpt366_chipsets[] = {
  1191. { /* 0: HPT36x */
  1192. .name = DRV_NAME,
  1193. .init_chipset = init_chipset_hpt366,
  1194. .init_hwif = init_hwif_hpt366,
  1195. .init_dma = init_dma_hpt366,
  1196. /*
  1197. * HPT36x chips have one channel per function and have
  1198. * both channel enable bits located differently and visible
  1199. * to both functions -- really stupid design decision... :-(
  1200. * Bit 4 is for the primary channel, bit 5 for the secondary.
  1201. */
  1202. .enablebits = {{0x50,0x10,0x10}, {0x54,0x04,0x04}},
  1203. .port_ops = &hpt3xx_port_ops,
  1204. .dma_ops = &hpt36x_dma_ops,
  1205. .host_flags = IDE_HFLAGS_HPT3XX | IDE_HFLAG_SINGLE,
  1206. .pio_mask = ATA_PIO4,
  1207. .mwdma_mask = ATA_MWDMA2,
  1208. },
  1209. { /* 1: HPT3xx */
  1210. .name = DRV_NAME,
  1211. .init_chipset = init_chipset_hpt366,
  1212. .init_hwif = init_hwif_hpt366,
  1213. .init_dma = init_dma_hpt366,
  1214. .enablebits = {{0x50,0x04,0x04}, {0x54,0x04,0x04}},
  1215. .port_ops = &hpt3xx_port_ops,
  1216. .dma_ops = &hpt37x_dma_ops,
  1217. .host_flags = IDE_HFLAGS_HPT3XX,
  1218. .pio_mask = ATA_PIO4,
  1219. .mwdma_mask = ATA_MWDMA2,
  1220. }
  1221. };
  1222. /**
  1223. * hpt366_init_one - called when an HPT366 is found
  1224. * @dev: the hpt366 device
  1225. * @id: the matching pci id
  1226. *
  1227. * Called when the PCI registration layer (or the IDE initialization)
  1228. * finds a device matching our IDE device tables.
  1229. */
  1230. static int hpt366_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  1231. {
  1232. const struct hpt_info *info = NULL;
  1233. struct hpt_info *dyn_info;
  1234. struct pci_dev *dev2 = NULL;
  1235. struct ide_port_info d;
  1236. u8 idx = id->driver_data;
  1237. u8 rev = dev->revision;
  1238. int ret;
  1239. if ((idx == 0 || idx == 4) && (PCI_FUNC(dev->devfn) & 1))
  1240. return -ENODEV;
  1241. switch (idx) {
  1242. case 0:
  1243. if (rev < 3)
  1244. info = &hpt36x;
  1245. else {
  1246. switch (min_t(u8, rev, 6)) {
  1247. case 3: info = &hpt370; break;
  1248. case 4: info = &hpt370a; break;
  1249. case 5: info = &hpt372; break;
  1250. case 6: info = &hpt372n; break;
  1251. }
  1252. idx++;
  1253. }
  1254. break;
  1255. case 1:
  1256. info = (rev > 1) ? &hpt372n : &hpt372a;
  1257. break;
  1258. case 2:
  1259. info = (rev > 1) ? &hpt302n : &hpt302;
  1260. break;
  1261. case 3:
  1262. hpt371_init(dev);
  1263. info = (rev > 1) ? &hpt371n : &hpt371;
  1264. break;
  1265. case 4:
  1266. info = &hpt374;
  1267. break;
  1268. case 5:
  1269. info = &hpt372n;
  1270. break;
  1271. }
  1272. printk(KERN_INFO DRV_NAME ": %s chipset detected\n", info->chip_name);
  1273. d = hpt366_chipsets[min_t(u8, idx, 1)];
  1274. d.udma_mask = info->udma_mask;
  1275. /* fixup ->dma_ops for HPT370/HPT370A */
  1276. if (info == &hpt370 || info == &hpt370a)
  1277. d.dma_ops = &hpt370_dma_ops;
  1278. if (info == &hpt36x || info == &hpt374)
  1279. dev2 = pci_get_slot(dev->bus, dev->devfn + 1);
  1280. dyn_info = kzalloc(sizeof(*dyn_info) * (dev2 ? 2 : 1), GFP_KERNEL);
  1281. if (dyn_info == NULL) {
  1282. printk(KERN_ERR "%s %s: out of memory!\n",
  1283. d.name, pci_name(dev));
  1284. pci_dev_put(dev2);
  1285. return -ENOMEM;
  1286. }
  1287. /*
  1288. * Copy everything from a static "template" structure
  1289. * to just allocated per-chip hpt_info structure.
  1290. */
  1291. memcpy(dyn_info, info, sizeof(*dyn_info));
  1292. if (dev2) {
  1293. memcpy(dyn_info + 1, info, sizeof(*dyn_info));
  1294. if (info == &hpt374)
  1295. hpt374_init(dev, dev2);
  1296. else {
  1297. if (hpt36x_init(dev, dev2))
  1298. d.host_flags &= ~IDE_HFLAG_NON_BOOTABLE;
  1299. }
  1300. ret = ide_pci_init_two(dev, dev2, &d, dyn_info);
  1301. if (ret < 0) {
  1302. pci_dev_put(dev2);
  1303. kfree(dyn_info);
  1304. }
  1305. return ret;
  1306. }
  1307. ret = ide_pci_init_one(dev, &d, dyn_info);
  1308. if (ret < 0)
  1309. kfree(dyn_info);
  1310. return ret;
  1311. }
  1312. static void hpt366_remove(struct pci_dev *dev)
  1313. {
  1314. struct ide_host *host = pci_get_drvdata(dev);
  1315. struct ide_info *info = host->host_priv;
  1316. struct pci_dev *dev2 = host->dev[1] ? to_pci_dev(host->dev[1]) : NULL;
  1317. ide_pci_remove(dev);
  1318. pci_dev_put(dev2);
  1319. kfree(info);
  1320. }
  1321. static const struct pci_device_id hpt366_pci_tbl[] = {
  1322. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT366), 0 },
  1323. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT372), 1 },
  1324. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT302), 2 },
  1325. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT371), 3 },
  1326. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT374), 4 },
  1327. { PCI_VDEVICE(TTI, PCI_DEVICE_ID_TTI_HPT372N), 5 },
  1328. { 0, },
  1329. };
  1330. MODULE_DEVICE_TABLE(pci, hpt366_pci_tbl);
  1331. static struct pci_driver hpt366_pci_driver = {
  1332. .name = "HPT366_IDE",
  1333. .id_table = hpt366_pci_tbl,
  1334. .probe = hpt366_init_one,
  1335. .remove = hpt366_remove,
  1336. .suspend = ide_pci_suspend,
  1337. .resume = ide_pci_resume,
  1338. };
  1339. static int __init hpt366_ide_init(void)
  1340. {
  1341. return ide_pci_register_driver(&hpt366_pci_driver);
  1342. }
  1343. static void __exit hpt366_ide_exit(void)
  1344. {
  1345. pci_unregister_driver(&hpt366_pci_driver);
  1346. }
  1347. module_init(hpt366_ide_init);
  1348. module_exit(hpt366_ide_exit);
  1349. MODULE_AUTHOR("Andre Hedrick");
  1350. MODULE_DESCRIPTION("PCI driver module for Highpoint HPT366 IDE");
  1351. MODULE_LICENSE("GPL");