plxsata_ide.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. /*
  2. * (C) Copyright 2005
  3. * Oxford Semiconductor Ltd
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,`
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/arch/clock.h>
  25. /**
  26. * SATA related definitions
  27. */
  28. #define ATA_PORT_CTL 0
  29. #define ATA_PORT_FEATURE 1
  30. #define ATA_PORT_NSECT 2
  31. #define ATA_PORT_LBAL 3
  32. #define ATA_PORT_LBAM 4
  33. #define ATA_PORT_LBAH 5
  34. #define ATA_PORT_DEVICE 6
  35. #define ATA_PORT_COMMAND 7
  36. /* The offsets to the SATA registers */
  37. #define SATA_ORB1_OFF 0
  38. #define SATA_ORB2_OFF 1
  39. #define SATA_ORB3_OFF 2
  40. #define SATA_ORB4_OFF 3
  41. #define SATA_ORB5_OFF 4
  42. #define SATA_FIS_ACCESS 11
  43. #define SATA_INT_STATUS_OFF 12 /* Read only */
  44. #define SATA_INT_CLR_OFF 12 /* Write only */
  45. #define SATA_INT_ENABLE_OFF 13 /* Read only */
  46. #define SATA_INT_ENABLE_SET_OFF 13 /* Write only */
  47. #define SATA_INT_ENABLE_CLR_OFF 14 /* Write only */
  48. #define SATA_VERSION_OFF 15
  49. #define SATA_CONTROL_OFF 23
  50. #define SATA_COMMAND_OFF 24
  51. #define SATA_PORT_CONTROL_OFF 25
  52. #define SATA_DRIVE_CONTROL_OFF 26
  53. /* The offsets to the link registers that are access in an asynchronous manner */
  54. #define SATA_LINK_DATA 28
  55. #define SATA_LINK_RD_ADDR 29
  56. #define SATA_LINK_WR_ADDR 30
  57. #define SATA_LINK_CONTROL 31
  58. /* SATA interrupt status register fields */
  59. #define SATA_INT_STATUS_EOC_RAW_BIT ( 0 + 16)
  60. #define SATA_INT_STATUS_ERROR_BIT ( 2 + 16)
  61. #define SATA_INT_STATUS_EOADT_RAW_BIT ( 1 + 16)
  62. /* SATA core command register commands */
  63. #define SATA_CMD_WRITE_TO_ORB_REGS 2
  64. #define SATA_CMD_WRITE_TO_ORB_REGS_NO_COMMAND 4
  65. #define SATA_CMD_BUSY_BIT 7
  66. #define SATA_SCTL_CLR_ERR 0x00000316UL
  67. #define SATA_LBAL_BIT 0
  68. #define SATA_LBAM_BIT 8
  69. #define SATA_LBAH_BIT 16
  70. #define SATA_HOB_LBAH_BIT 24
  71. #define SATA_DEVICE_BIT 24
  72. #define SATA_NSECT_BIT 0
  73. #define SATA_HOB_NSECT_BIT 8
  74. #define SATA_LBA32_BIT 0
  75. #define SATA_LBA40_BIT 8
  76. #define SATA_FEATURE_BIT 16
  77. #define SATA_COMMAND_BIT 24
  78. #define SATA_CTL_BIT 24
  79. /* ATA status (7) register field definitions */
  80. #define ATA_STATUS_BSY_BIT 7
  81. #define ATA_STATUS_DRDY_BIT 6
  82. #define ATA_STATUS_DF_BIT 5
  83. #define ATA_STATUS_DRQ_BIT 3
  84. #define ATA_STATUS_ERR_BIT 0
  85. /* ATA device (6) register field definitions */
  86. #define ATA_DEVICE_FIXED_MASK 0xA0
  87. #define ATA_DEVICE_DRV_BIT 4
  88. #define ATA_DEVICE_DRV_NUM_BITS 1
  89. #define ATA_DEVICE_LBA_BIT 6
  90. /* ATA Command register initiated commands */
  91. #define ATA_CMD_INIT 0x91
  92. #define ATA_CMD_IDENT 0xEC
  93. #define SATA_STD_ASYNC_REGS_OFF 0x20
  94. #define SATA_SCR_STATUS 0
  95. #define SATA_SCR_ERROR 1
  96. #define SATA_SCR_CONTROL 2
  97. #define SATA_SCR_ACTIVE 3
  98. #define SATA_SCR_NOTIFICAION 4
  99. #define SATA_BURST_BUF_FORCE_EOT_BIT 0
  100. #define SATA_BURST_BUF_DATA_INJ_ENABLE_BIT 1
  101. #define SATA_BURST_BUF_DIR_BIT 2
  102. #define SATA_BURST_BUF_DATA_INJ_END_BIT 3
  103. #define SATA_BURST_BUF_FIFO_DIS_BIT 4
  104. #define SATA_BURST_BUF_DIS_DREQ_BIT 5
  105. #define SATA_BURST_BUF_DREQ_BIT 6
  106. #define SATA_OPCODE_MASK 0x3
  107. #define SATA_DMA_CHANNEL 0
  108. #define DMA_CTRL_STATUS (0x0)
  109. #define DMA_BASE_SRC_ADR (0x4)
  110. #define DMA_BASE_DST_ADR (0x8)
  111. #define DMA_BYTE_CNT (0xC)
  112. #define DMA_CURRENT_SRC_ADR (0x10)
  113. #define DMA_CURRENT_DST_ADR (0x14)
  114. #define DMA_CURRENT_BYTE_CNT (0x18)
  115. #define DMA_INTR_ID (0x1C)
  116. #define DMA_INTR_CLEAR_REG (DMA_CURRENT_SRC_ADR)
  117. #define DMA_CALC_REG_ADR(channel, register) ((volatile u32*)(DMA_BASE + ((channel) << 5) + (register)))
  118. #define DMA_CTRL_STATUS_FAIR_SHARE_ARB (1 << 0)
  119. #define DMA_CTRL_STATUS_IN_PROGRESS (1 << 1)
  120. #define DMA_CTRL_STATUS_SRC_DREQ_MASK (0x0000003C)
  121. #define DMA_CTRL_STATUS_SRC_DREQ_SHIFT (2)
  122. #define DMA_CTRL_STATUS_DEST_DREQ_MASK (0x000003C0)
  123. #define DMA_CTRL_STATUS_DEST_DREQ_SHIFT (6)
  124. #define DMA_CTRL_STATUS_INTR (1 << 10)
  125. #define DMA_CTRL_STATUS_NXT_FREE (1 << 11)
  126. #define DMA_CTRL_STATUS_RESET (1 << 12)
  127. #define DMA_CTRL_STATUS_DIR_MASK (0x00006000)
  128. #define DMA_CTRL_STATUS_DIR_SHIFT (13)
  129. #define DMA_CTRL_STATUS_SRC_ADR_MODE (1 << 15)
  130. #define DMA_CTRL_STATUS_DEST_ADR_MODE (1 << 16)
  131. #define DMA_CTRL_STATUS_TRANSFER_MODE_A (1 << 17)
  132. #define DMA_CTRL_STATUS_TRANSFER_MODE_B (1 << 18)
  133. #define DMA_CTRL_STATUS_SRC_WIDTH_MASK (0x00380000)
  134. #define DMA_CTRL_STATUS_SRC_WIDTH_SHIFT (19)
  135. #define DMA_CTRL_STATUS_DEST_WIDTH_MASK (0x01C00000)
  136. #define DMA_CTRL_STATUS_DEST_WIDTH_SHIFT (22)
  137. #define DMA_CTRL_STATUS_PAUSE (1 << 25)
  138. #define DMA_CTRL_STATUS_INTERRUPT_ENABLE (1 << 26)
  139. #define DMA_CTRL_STATUS_SOURCE_ADDRESS_FIXED (1 << 27)
  140. #define DMA_CTRL_STATUS_DESTINATION_ADDRESS_FIXED (1 << 28)
  141. #define DMA_CTRL_STATUS_STARVE_LOW_PRIORITY (1 << 29)
  142. #define DMA_CTRL_STATUS_INTR_CLEAR_ENABLE (1 << 30)
  143. #define DMA_BYTE_CNT_MASK ((1 << 21) - 1)
  144. #define DMA_BYTE_CNT_WR_EOT_MASK (1 << 30)
  145. #define DMA_BYTE_CNT_RD_EOT_MASK (1 << 31)
  146. #define DMA_BYTE_CNT_BURST_MASK (1 << 28)
  147. #define MAKE_FIELD(value, num_bits, bit_num) (((value) & ((1 << (num_bits)) - 1)) << (bit_num))
  148. typedef enum oxnas_dma_mode {
  149. OXNAS_DMA_MODE_FIXED, OXNAS_DMA_MODE_INC
  150. } oxnas_dma_mode_t;
  151. typedef enum oxnas_dma_direction {
  152. OXNAS_DMA_TO_DEVICE, OXNAS_DMA_FROM_DEVICE
  153. } oxnas_dma_direction_t;
  154. /* The available buses to which the DMA controller is attached */
  155. typedef enum oxnas_dma_transfer_bus {
  156. OXNAS_DMA_SIDE_A, OXNAS_DMA_SIDE_B
  157. } oxnas_dma_transfer_bus_t;
  158. /* Direction of data flow between the DMA controller's pair of interfaces */
  159. typedef enum oxnas_dma_transfer_direction {
  160. OXNAS_DMA_A_TO_A, OXNAS_DMA_B_TO_A, OXNAS_DMA_A_TO_B, OXNAS_DMA_B_TO_B
  161. } oxnas_dma_transfer_direction_t;
  162. /* The available data widths */
  163. typedef enum oxnas_dma_transfer_width {
  164. OXNAS_DMA_TRANSFER_WIDTH_8BITS,
  165. OXNAS_DMA_TRANSFER_WIDTH_16BITS,
  166. OXNAS_DMA_TRANSFER_WIDTH_32BITS
  167. } oxnas_dma_transfer_width_t;
  168. /* The mode of the DMA transfer */
  169. typedef enum oxnas_dma_transfer_mode {
  170. OXNAS_DMA_TRANSFER_MODE_SINGLE, OXNAS_DMA_TRANSFER_MODE_BURST
  171. } oxnas_dma_transfer_mode_t;
  172. /* The available transfer targets */
  173. typedef enum oxnas_dma_dreq {
  174. OXNAS_DMA_DREQ_SATA = 0, OXNAS_DMA_DREQ_MEMORY = 15
  175. } oxnas_dma_dreq_t;
  176. typedef struct oxnas_dma_device_settings {
  177. unsigned long address_;
  178. unsigned fifo_size_; // Chained transfers must take account of FIFO offset at end of previous transfer
  179. unsigned char dreq_;
  180. unsigned read_eot_ :1;
  181. unsigned read_final_eot_ :1;
  182. unsigned write_eot_ :1;
  183. unsigned write_final_eot_ :1;
  184. unsigned bus_ :1;
  185. unsigned width_ :2;
  186. unsigned transfer_mode_ :1;
  187. unsigned address_mode_ :1;
  188. unsigned address_really_fixed_ :1;
  189. } oxnas_dma_device_settings_t;
  190. static const int MAX_NO_ERROR_LOOPS = 100000; /* 1 second in units of 10uS */
  191. static const int MAX_DMA_XFER_LOOPS = 300000; /* 30 seconds in units of 100uS */
  192. static const int MAX_DMA_ABORT_LOOPS = 10000; /* 0.1 second in units of 10uS */
  193. static const int MAX_SRC_READ_LOOPS = 10000; /* 0.1 second in units of 10uS */
  194. static const int MAX_SRC_WRITE_LOOPS = 10000; /* 0.1 second in units of 10uS */
  195. static const int MAX_NOT_BUSY_LOOPS = 10000; /* 1 second in units of 100uS */
  196. /* The internal SATA drive on which we should attempt to find partitions */
  197. static volatile u32* sata_regs_base[2] = { (volatile u32*) SATA_0_REGS_BASE,
  198. (volatile u32*) SATA_1_REGS_BASE,
  199. };
  200. static u32 wr_sata_orb1[2] = { 0, 0 };
  201. static u32 wr_sata_orb2[2] = { 0, 0 };
  202. static u32 wr_sata_orb3[2] = { 0, 0 };
  203. static u32 wr_sata_orb4[2] = { 0, 0 };
  204. #ifdef CONFIG_LBA48
  205. /* need keeping a record of NSECT LBAL LBAM LBAH ide_outb values for lba48 support */
  206. #define OUT_HISTORY_BASE ATA_PORT_NSECT
  207. #define OUT_HISTORY_MAX ATA_PORT_LBAH
  208. static unsigned char out_history[2][OUT_HISTORY_MAX - OUT_HISTORY_BASE + 1] = {};
  209. #endif
  210. static oxnas_dma_device_settings_t oxnas_sata_dma_settings = { .address_ =
  211. SATA_DATA_BASE, .fifo_size_ = 16, .dreq_ = OXNAS_DMA_DREQ_SATA,
  212. .read_eot_ = 0, .read_final_eot_ = 1, .write_eot_ = 0,
  213. .write_final_eot_ = 1, .bus_ = OXNAS_DMA_SIDE_B, .width_ =
  214. OXNAS_DMA_TRANSFER_WIDTH_32BITS, .transfer_mode_ =
  215. OXNAS_DMA_TRANSFER_MODE_BURST, .address_mode_ =
  216. OXNAS_DMA_MODE_FIXED, .address_really_fixed_ = 0 };
  217. oxnas_dma_device_settings_t oxnas_ram_dma_settings = { .address_ = 0,
  218. .fifo_size_ = 0, .dreq_ = OXNAS_DMA_DREQ_MEMORY, .read_eot_ = 1,
  219. .read_final_eot_ = 1, .write_eot_ = 1, .write_final_eot_ = 1,
  220. .bus_ = OXNAS_DMA_SIDE_A, .width_ =
  221. OXNAS_DMA_TRANSFER_WIDTH_32BITS, .transfer_mode_ =
  222. OXNAS_DMA_TRANSFER_MODE_BURST, .address_mode_ =
  223. OXNAS_DMA_MODE_FIXED, .address_really_fixed_ = 1 };
  224. static void xfer_wr_shadow_to_orbs(int device)
  225. {
  226. *(sata_regs_base[device] + SATA_ORB1_OFF) = wr_sata_orb1[device];
  227. *(sata_regs_base[device] + SATA_ORB2_OFF) = wr_sata_orb2[device];
  228. *(sata_regs_base[device] + SATA_ORB3_OFF) = wr_sata_orb3[device];
  229. *(sata_regs_base[device] + SATA_ORB4_OFF) = wr_sata_orb4[device];
  230. }
  231. static inline void device_select(int device)
  232. {
  233. /* master/slave has no meaning to SATA core */
  234. }
  235. static int disk_present[CONFIG_SYS_IDE_MAXDEVICE];
  236. #include <ata.h>
  237. unsigned char ide_inb(int device, int port)
  238. {
  239. unsigned char val = 0;
  240. /* Only permit accesses to disks found to be present during ide_preinit() */
  241. if (!disk_present[device]) {
  242. return ATA_STAT_FAULT;
  243. }
  244. device_select(device);
  245. switch (port) {
  246. case ATA_PORT_CTL:
  247. val = (*(sata_regs_base[device] + SATA_ORB4_OFF)
  248. & (0xFFUL << SATA_CTL_BIT)) >> SATA_CTL_BIT;
  249. break;
  250. case ATA_PORT_FEATURE:
  251. val = (*(sata_regs_base[device] + SATA_ORB2_OFF)
  252. & (0xFFUL << SATA_FEATURE_BIT)) >> SATA_FEATURE_BIT;
  253. break;
  254. case ATA_PORT_NSECT:
  255. val = (*(sata_regs_base[device] + SATA_ORB2_OFF)
  256. & (0xFFUL << SATA_NSECT_BIT)) >> SATA_NSECT_BIT;
  257. break;
  258. case ATA_PORT_LBAL:
  259. val = (*(sata_regs_base[device] + SATA_ORB3_OFF)
  260. & (0xFFUL << SATA_LBAL_BIT)) >> SATA_LBAL_BIT;
  261. break;
  262. case ATA_PORT_LBAM:
  263. val = (*(sata_regs_base[device] + SATA_ORB3_OFF)
  264. & (0xFFUL << SATA_LBAM_BIT)) >> SATA_LBAM_BIT;
  265. break;
  266. case ATA_PORT_LBAH:
  267. val = (*(sata_regs_base[device] + SATA_ORB3_OFF)
  268. & (0xFFUL << SATA_LBAH_BIT)) >> SATA_LBAH_BIT;
  269. break;
  270. case ATA_PORT_DEVICE:
  271. val = (*(sata_regs_base[device] + SATA_ORB3_OFF)
  272. & (0xFFUL << SATA_HOB_LBAH_BIT)) >> SATA_HOB_LBAH_BIT;
  273. val |= (*(sata_regs_base[device] + SATA_ORB1_OFF)
  274. & (0xFFUL << SATA_DEVICE_BIT)) >> SATA_DEVICE_BIT;
  275. break;
  276. case ATA_PORT_COMMAND:
  277. val = (*(sata_regs_base[device] + SATA_ORB2_OFF)
  278. & (0xFFUL << SATA_COMMAND_BIT)) >> SATA_COMMAND_BIT;
  279. val |= ATA_STAT_DRQ;
  280. break;
  281. default:
  282. printf("ide_inb() Unknown port = %d\n", port);
  283. break;
  284. }
  285. // printf("inb: %d:%01x => %02x\n", device, port, val);
  286. return val;
  287. }
  288. /**
  289. * Possible that ATA status will not become no-error, so must have timeout
  290. * @returns An int which is zero on error
  291. */
  292. static inline int wait_no_error(int device)
  293. {
  294. int status = 0;
  295. /* Check for ATA core error */
  296. if (*(sata_regs_base[device] + SATA_INT_STATUS_OFF)
  297. & (1 << SATA_INT_STATUS_ERROR_BIT)) {
  298. printf("wait_no_error() SATA core flagged error\n");
  299. } else {
  300. int loops = MAX_NO_ERROR_LOOPS;
  301. do {
  302. /* Check for ATA device error */
  303. if (!(ide_inb(device, ATA_PORT_COMMAND)
  304. & (1 << ATA_STATUS_ERR_BIT))) {
  305. status = 1;
  306. break;
  307. }
  308. udelay(10);
  309. } while (--loops);
  310. if (!loops) {
  311. printf("wait_no_error() Timed out of wait for SATA no-error condition\n");
  312. }
  313. }
  314. return status;
  315. }
  316. /**
  317. * Expect SATA command to always finish, perhaps with error
  318. * @returns An int which is zero on error
  319. */
  320. static inline int wait_sata_command_not_busy(int device)
  321. {
  322. /* Wait for data to be available */
  323. int status = 0;
  324. int loops = MAX_NOT_BUSY_LOOPS;
  325. do {
  326. if (!(*(sata_regs_base[device] + SATA_COMMAND_OFF)
  327. & (1 << SATA_CMD_BUSY_BIT))) {
  328. status = 1;
  329. break;
  330. }
  331. udelay(100);
  332. } while (--loops);
  333. if (!loops) {
  334. printf("wait_sata_command_not_busy() Timed out of wait for SATA command to finish\n");
  335. }
  336. return status;
  337. }
  338. void ide_outb(int device, int port, unsigned char val)
  339. {
  340. typedef enum send_method {
  341. SEND_NONE, SEND_SIMPLE, SEND_CMD, SEND_CTL,
  342. } send_method_t;
  343. /* Only permit accesses to disks found to be present during ide_preinit() */
  344. if (!disk_present[device]) {
  345. return;
  346. }
  347. // printf("outb: %d:%01x <= %02x\n", device, port, val);
  348. device_select(device);
  349. #ifdef CONFIG_LBA48
  350. if (port >= OUT_HISTORY_BASE && port <= OUT_HISTORY_MAX) {
  351. out_history[0][port - OUT_HISTORY_BASE] =
  352. out_history[1][port - OUT_HISTORY_BASE];
  353. out_history[1][port - OUT_HISTORY_BASE] = val;
  354. }
  355. #endif
  356. send_method_t send_regs = SEND_NONE;
  357. switch (port) {
  358. case ATA_PORT_CTL:
  359. wr_sata_orb4[device] &= ~(0xFFUL << SATA_CTL_BIT);
  360. wr_sata_orb4[device] |= (val << SATA_CTL_BIT);
  361. send_regs = SEND_CTL;
  362. break;
  363. case ATA_PORT_FEATURE:
  364. wr_sata_orb2[device] &= ~(0xFFUL << SATA_FEATURE_BIT);
  365. wr_sata_orb2[device] |= (val << SATA_FEATURE_BIT);
  366. send_regs = SEND_SIMPLE;
  367. break;
  368. case ATA_PORT_NSECT:
  369. wr_sata_orb2[device] &= ~(0xFFUL << SATA_NSECT_BIT);
  370. wr_sata_orb2[device] |= (val << SATA_NSECT_BIT);
  371. send_regs = SEND_SIMPLE;
  372. break;
  373. case ATA_PORT_LBAL:
  374. wr_sata_orb3[device] &= ~(0xFFUL << SATA_LBAL_BIT);
  375. wr_sata_orb3[device] |= (val << SATA_LBAL_BIT);
  376. send_regs = SEND_SIMPLE;
  377. break;
  378. case ATA_PORT_LBAM:
  379. wr_sata_orb3[device] &= ~(0xFFUL << SATA_LBAM_BIT);
  380. wr_sata_orb3[device] |= (val << SATA_LBAM_BIT);
  381. send_regs = SEND_SIMPLE;
  382. break;
  383. case ATA_PORT_LBAH:
  384. wr_sata_orb3[device] &= ~(0xFFUL << SATA_LBAH_BIT);
  385. wr_sata_orb3[device] |= (val << SATA_LBAH_BIT);
  386. send_regs = SEND_SIMPLE;
  387. break;
  388. case ATA_PORT_DEVICE:
  389. wr_sata_orb1[device] &= ~(0xFFUL << SATA_DEVICE_BIT);
  390. wr_sata_orb1[device] |= (val << SATA_DEVICE_BIT);
  391. send_regs = SEND_SIMPLE;
  392. break;
  393. case ATA_PORT_COMMAND:
  394. wr_sata_orb2[device] &= ~(0xFFUL << SATA_COMMAND_BIT);
  395. wr_sata_orb2[device] |= (val << SATA_COMMAND_BIT);
  396. send_regs = SEND_CMD;
  397. #ifdef CONFIG_LBA48
  398. if (val == ATA_CMD_READ_EXT || val == ATA_CMD_WRITE_EXT)
  399. {
  400. /* fill high bytes of LBA48 && NSECT */
  401. wr_sata_orb2[device] &= ~(0xFFUL << SATA_HOB_NSECT_BIT);
  402. wr_sata_orb2[device] |=
  403. (out_history[0][ATA_PORT_NSECT - OUT_HISTORY_BASE] << SATA_HOB_NSECT_BIT);
  404. wr_sata_orb3[device] &= ~(0xFFUL << SATA_HOB_LBAH_BIT);
  405. wr_sata_orb3[device] |=
  406. (out_history[0][ATA_PORT_LBAL - OUT_HISTORY_BASE] << SATA_HOB_LBAH_BIT);
  407. wr_sata_orb4[device] &= ~(0xFFUL << SATA_LBA32_BIT);
  408. wr_sata_orb4[device] |=
  409. (out_history[0][ATA_PORT_LBAM - OUT_HISTORY_BASE] << SATA_LBA32_BIT);
  410. wr_sata_orb4[device] &= ~(0xFFUL << SATA_LBA40_BIT);
  411. wr_sata_orb4[device] |=
  412. (out_history[0][ATA_PORT_LBAH - OUT_HISTORY_BASE] << SATA_LBA40_BIT);
  413. }
  414. #endif
  415. break;
  416. default:
  417. printf("ide_outb() Unknown port = %d\n", port);
  418. }
  419. u32 command;
  420. switch (send_regs) {
  421. case SEND_CMD:
  422. wait_sata_command_not_busy(device);
  423. command = *(sata_regs_base[device] + SATA_COMMAND_OFF);
  424. command &= ~SATA_OPCODE_MASK;
  425. command |= SATA_CMD_WRITE_TO_ORB_REGS;
  426. xfer_wr_shadow_to_orbs(device);
  427. wait_sata_command_not_busy(device);
  428. *(sata_regs_base[device] + SATA_COMMAND_OFF) = command;
  429. if (!wait_no_error(device)) {
  430. printf("ide_outb() Wait for ATA no-error timed-out\n");
  431. }
  432. break;
  433. case SEND_CTL:
  434. wait_sata_command_not_busy(device);
  435. command = *(sata_regs_base[device] + SATA_COMMAND_OFF);
  436. command &= ~SATA_OPCODE_MASK;
  437. command |= SATA_CMD_WRITE_TO_ORB_REGS_NO_COMMAND;
  438. xfer_wr_shadow_to_orbs(device);
  439. wait_sata_command_not_busy(device);
  440. *(sata_regs_base[device] + SATA_COMMAND_OFF) = command;
  441. if (!wait_no_error(device)) {
  442. printf("ide_outb() Wait for ATA no-error timed-out\n");
  443. }
  444. break;
  445. default:
  446. break;
  447. }
  448. }
  449. static u32 encode_start(u32 ctrl_status)
  450. {
  451. return ctrl_status & ~DMA_CTRL_STATUS_PAUSE;
  452. }
  453. /* start a paused DMA transfer in channel 0 of the SATA DMA core */
  454. static void dma_start(void)
  455. {
  456. unsigned int reg;
  457. reg = readl(SATA_DMA_REGS_BASE + DMA_CTRL_STATUS);
  458. reg = encode_start(reg);
  459. writel(reg, SATA_DMA_REGS_BASE + DMA_CTRL_STATUS);
  460. }
  461. static unsigned long encode_control_status(
  462. oxnas_dma_device_settings_t* src_settings,
  463. oxnas_dma_device_settings_t* dst_settings)
  464. {
  465. unsigned long ctrl_status;
  466. oxnas_dma_transfer_direction_t direction;
  467. ctrl_status = DMA_CTRL_STATUS_PAUSE; // Paused
  468. ctrl_status |= DMA_CTRL_STATUS_FAIR_SHARE_ARB; // High priority
  469. ctrl_status |= (src_settings->dreq_ << DMA_CTRL_STATUS_SRC_DREQ_SHIFT); // Dreq
  470. ctrl_status |= (dst_settings->dreq_ << DMA_CTRL_STATUS_DEST_DREQ_SHIFT); // Dreq
  471. ctrl_status &= ~DMA_CTRL_STATUS_RESET; // !RESET
  472. // Use new interrupt clearing register
  473. ctrl_status |= DMA_CTRL_STATUS_INTR_CLEAR_ENABLE;
  474. // Setup the transfer direction and burst/single mode for the two DMA busses
  475. if (src_settings->bus_ == OXNAS_DMA_SIDE_A) {
  476. // Set the burst/single mode for bus A based on src device's settings
  477. if (src_settings->transfer_mode_
  478. == OXNAS_DMA_TRANSFER_MODE_BURST) {
  479. ctrl_status |= DMA_CTRL_STATUS_TRANSFER_MODE_A;
  480. } else {
  481. ctrl_status &= ~DMA_CTRL_STATUS_TRANSFER_MODE_A;
  482. }
  483. if (dst_settings->bus_ == OXNAS_DMA_SIDE_A) {
  484. direction = OXNAS_DMA_A_TO_A;
  485. } else {
  486. direction = OXNAS_DMA_A_TO_B;
  487. // Set the burst/single mode for bus B based on dst device's settings
  488. if (dst_settings->transfer_mode_
  489. == OXNAS_DMA_TRANSFER_MODE_BURST) {
  490. ctrl_status |= DMA_CTRL_STATUS_TRANSFER_MODE_B;
  491. } else {
  492. ctrl_status &= ~DMA_CTRL_STATUS_TRANSFER_MODE_B;
  493. }
  494. }
  495. } else {
  496. // Set the burst/single mode for bus B based on src device's settings
  497. if (src_settings->transfer_mode_
  498. == OXNAS_DMA_TRANSFER_MODE_BURST) {
  499. ctrl_status |= DMA_CTRL_STATUS_TRANSFER_MODE_B;
  500. } else {
  501. ctrl_status &= ~DMA_CTRL_STATUS_TRANSFER_MODE_B;
  502. }
  503. if (dst_settings->bus_ == OXNAS_DMA_SIDE_A) {
  504. direction = OXNAS_DMA_B_TO_A;
  505. // Set the burst/single mode for bus A based on dst device's settings
  506. if (dst_settings->transfer_mode_
  507. == OXNAS_DMA_TRANSFER_MODE_BURST) {
  508. ctrl_status |= DMA_CTRL_STATUS_TRANSFER_MODE_A;
  509. } else {
  510. ctrl_status &= ~DMA_CTRL_STATUS_TRANSFER_MODE_A;
  511. }
  512. } else {
  513. direction = OXNAS_DMA_B_TO_B;
  514. }
  515. }
  516. ctrl_status |= (direction << DMA_CTRL_STATUS_DIR_SHIFT);
  517. // Setup source address mode fixed or increment
  518. if (src_settings->address_mode_ == OXNAS_DMA_MODE_FIXED) {
  519. // Fixed address
  520. ctrl_status &= ~(DMA_CTRL_STATUS_SRC_ADR_MODE);
  521. // Set up whether fixed address is _really_ fixed
  522. if (src_settings->address_really_fixed_) {
  523. ctrl_status |= DMA_CTRL_STATUS_SOURCE_ADDRESS_FIXED;
  524. } else {
  525. ctrl_status &= ~DMA_CTRL_STATUS_SOURCE_ADDRESS_FIXED;
  526. }
  527. } else {
  528. // Incrementing address
  529. ctrl_status |= DMA_CTRL_STATUS_SRC_ADR_MODE;
  530. ctrl_status &= ~DMA_CTRL_STATUS_SOURCE_ADDRESS_FIXED;
  531. }
  532. // Setup destination address mode fixed or increment
  533. if (dst_settings->address_mode_ == OXNAS_DMA_MODE_FIXED) {
  534. // Fixed address
  535. ctrl_status &= ~(DMA_CTRL_STATUS_DEST_ADR_MODE);
  536. // Set up whether fixed address is _really_ fixed
  537. if (dst_settings->address_really_fixed_) {
  538. ctrl_status |=
  539. DMA_CTRL_STATUS_DESTINATION_ADDRESS_FIXED;
  540. } else {
  541. ctrl_status &=
  542. ~DMA_CTRL_STATUS_DESTINATION_ADDRESS_FIXED;
  543. }
  544. } else {
  545. // Incrementing address
  546. ctrl_status |= DMA_CTRL_STATUS_DEST_ADR_MODE;
  547. ctrl_status &= ~DMA_CTRL_STATUS_DESTINATION_ADDRESS_FIXED;
  548. }
  549. // Set up the width of the transfers on the DMA buses
  550. ctrl_status |=
  551. (src_settings->width_ << DMA_CTRL_STATUS_SRC_WIDTH_SHIFT);
  552. ctrl_status |=
  553. (dst_settings->width_ << DMA_CTRL_STATUS_DEST_WIDTH_SHIFT);
  554. // Setup the priority arbitration scheme
  555. ctrl_status &= ~DMA_CTRL_STATUS_STARVE_LOW_PRIORITY; // !Starve low priority
  556. return ctrl_status;
  557. }
  558. static u32 encode_final_eot(oxnas_dma_device_settings_t* src_settings,
  559. oxnas_dma_device_settings_t* dst_settings,
  560. unsigned long length)
  561. {
  562. // Write the length, with EOT configuration for a final transfer
  563. unsigned long encoded = length;
  564. if (dst_settings->write_final_eot_) {
  565. encoded |= DMA_BYTE_CNT_WR_EOT_MASK;
  566. } else {
  567. encoded &= ~DMA_BYTE_CNT_WR_EOT_MASK;
  568. }
  569. if (src_settings->read_final_eot_) {
  570. encoded |= DMA_BYTE_CNT_RD_EOT_MASK;
  571. } else {
  572. encoded &= ~DMA_BYTE_CNT_RD_EOT_MASK;
  573. }
  574. /* if((src_settings->transfer_mode_) ||
  575. (src_settings->transfer_mode_)) {
  576. encoded |= DMA_BYTE_CNT_BURST_MASK;
  577. } else {
  578. encoded &= ~DMA_BYTE_CNT_BURST_MASK;
  579. }*/
  580. return encoded;
  581. }
  582. static void dma_start_write(const ulong* buffer, int num_bytes)
  583. {
  584. // Assemble complete memory settings
  585. oxnas_dma_device_settings_t mem_settings = oxnas_ram_dma_settings;
  586. mem_settings.address_ = (unsigned long) buffer;
  587. mem_settings.address_mode_ = OXNAS_DMA_MODE_INC;
  588. writel(encode_control_status(&mem_settings, &oxnas_sata_dma_settings),
  589. SATA_DMA_REGS_BASE + DMA_CTRL_STATUS);
  590. writel(mem_settings.address_, SATA_DMA_REGS_BASE + DMA_BASE_SRC_ADR);
  591. writel(oxnas_sata_dma_settings.address_,
  592. SATA_DMA_REGS_BASE + DMA_BASE_DST_ADR);
  593. writel(encode_final_eot(&mem_settings, &oxnas_sata_dma_settings,
  594. num_bytes),
  595. SATA_DMA_REGS_BASE + DMA_BYTE_CNT);
  596. dma_start();
  597. }
  598. static void dma_start_read(ulong* buffer, int num_bytes)
  599. {
  600. // Assemble complete memory settings
  601. oxnas_dma_device_settings_t mem_settings = oxnas_ram_dma_settings;
  602. mem_settings.address_ = (unsigned long) buffer;
  603. mem_settings.address_mode_ = OXNAS_DMA_MODE_INC;
  604. writel(encode_control_status(&oxnas_sata_dma_settings, &mem_settings),
  605. SATA_DMA_REGS_BASE + DMA_CTRL_STATUS);
  606. writel(oxnas_sata_dma_settings.address_,
  607. SATA_DMA_REGS_BASE + DMA_BASE_SRC_ADR);
  608. writel(mem_settings.address_, SATA_DMA_REGS_BASE + DMA_BASE_DST_ADR);
  609. writel(encode_final_eot(&oxnas_sata_dma_settings, &mem_settings,
  610. num_bytes),
  611. SATA_DMA_REGS_BASE + DMA_BYTE_CNT);
  612. dma_start();
  613. }
  614. static inline int dma_busy(void)
  615. {
  616. return readl(SATA_DMA_REGS_BASE + DMA_CTRL_STATUS)
  617. & DMA_CTRL_STATUS_IN_PROGRESS;
  618. }
  619. static int wait_dma_not_busy(int device)
  620. {
  621. unsigned int cleanup_required = 0;
  622. /* Poll for DMA completion */
  623. int loops = MAX_DMA_XFER_LOOPS;
  624. do {
  625. if (!dma_busy()) {
  626. break;
  627. }
  628. udelay(100);
  629. } while (--loops);
  630. if (!loops) {
  631. printf("wait_dma_not_busy() Timed out of wait for DMA not busy\n");
  632. cleanup_required = 1;
  633. }
  634. if (cleanup_required) {
  635. /* Abort DMA to make sure it has finished. */
  636. unsigned int ctrl_status = readl(
  637. SATA_DMA_CHANNEL + DMA_CTRL_STATUS);
  638. ctrl_status |= DMA_CTRL_STATUS_RESET;
  639. writel(ctrl_status, SATA_DMA_CHANNEL + DMA_CTRL_STATUS);
  640. // Wait for the channel to become idle - should be quick as should
  641. // finish after the next AHB single or burst transfer
  642. loops = MAX_DMA_ABORT_LOOPS;
  643. do {
  644. if (!dma_busy()) {
  645. break;
  646. }
  647. udelay(10);
  648. } while (--loops);
  649. if (!loops) {
  650. printf("wait_dma_not_busy() Timed out of wait for DMA channel abort\n");
  651. } else {
  652. /* Successfully cleanup the DMA channel */
  653. cleanup_required = 0;
  654. }
  655. // Deassert reset for the channel
  656. ctrl_status = readl(SATA_DMA_CHANNEL + DMA_CTRL_STATUS);
  657. ctrl_status &= ~DMA_CTRL_STATUS_RESET;
  658. writel(ctrl_status, SATA_DMA_CHANNEL + DMA_CTRL_STATUS);
  659. }
  660. return !cleanup_required;
  661. }
  662. /**
  663. * Possible that ATA status will not become not-busy, so must have timeout
  664. */
  665. static unsigned int wait_not_busy(int device, unsigned long timeout_secs)
  666. {
  667. int busy = 1;
  668. unsigned long loops = (timeout_secs * 1000) / 50;
  669. do {
  670. // Test the ATA status register BUSY flag
  671. if (!((*(sata_regs_base[device] + SATA_ORB2_OFF)
  672. >> SATA_COMMAND_BIT) & (1UL << ATA_STATUS_BSY_BIT))) {
  673. /* Not busy, so stop polling */
  674. busy = 0;
  675. break;
  676. }
  677. // Wait for 50mS before sampling ATA status register again
  678. udelay(50000);
  679. } while (--loops);
  680. return busy;
  681. }
  682. void ide_output_data(int device, const ulong *sect_buf, int words)
  683. {
  684. /* Only permit accesses to disks found to be present during ide_preinit() */
  685. if (!disk_present[device]) {
  686. return;
  687. }
  688. /* Select the required internal SATA drive */
  689. device_select(device);
  690. /* Start the DMA channel sending data from the passed buffer to the SATA core */
  691. dma_start_write(sect_buf, words << 2);
  692. /* Don't know why we need this delay, but without it the wait for DMA not
  693. busy times soemtimes out, e.g. when saving environment to second disk */
  694. udelay(1000);
  695. /* Wait for DMA to finish */
  696. if (!wait_dma_not_busy(device)) {
  697. printf("Timed out of wait for DMA channel for SATA device %d to have in-progress clear\n",
  698. device);
  699. }
  700. /* Sata core should finish after DMA */
  701. if (wait_not_busy(device, 30)) {
  702. printf("Timed out of wait for SATA device %d to have BUSY clear\n",
  703. device);
  704. }
  705. if (!wait_no_error(device)) {
  706. printf("oxnas_sata_output_data() Wait for ATA no-error timed-out\n");
  707. }
  708. }
  709. #define SATA_DM_DBG1 (SATA_HOST_REGS_BASE + 0)
  710. #define SATA_DATACOUNT_PORT0 (SATA_HOST_REGS_BASE + 0x10)
  711. #define SATA_DATACOUNT_PORT1 (SATA_HOST_REGS_BASE + 0x14)
  712. #define SATA_DATA_MUX_RAM0 (SATA_HOST_REGS_BASE + 0x8000)
  713. #define SATA_DATA_MUX_RAM1 (SATA_HOST_REGS_BASE + 0xA000)
  714. /* Sata core debug1 register bits */
  715. #define SATA_CORE_PORT0_DATA_DIR_BIT 20
  716. #define SATA_CORE_PORT1_DATA_DIR_BIT 21
  717. #define SATA_CORE_PORT0_DATA_DIR (1 << SATA_CORE_PORT0_DATA_DIR_BIT)
  718. #define SATA_CORE_PORT1_DATA_DIR (1 << SATA_CORE_PORT1_DATA_DIR_BIT)
  719. /**
  720. * Ref bug-6320
  721. *
  722. * This code is a work around for a DMA hardware bug that will repeat the
  723. * penultimate 8-bytes on some reads. This code will check that the amount
  724. * of data transferred is a multiple of 512 bytes, if not the in it will
  725. * fetch the correct data from a buffer in the SATA core and copy it into
  726. * memory.
  727. *
  728. */
  729. static void sata_bug_6320_workaround(int port, ulong *candidate)
  730. {
  731. int is_read;
  732. int quads_transferred;
  733. int remainder;
  734. int sector_quads_remaining;
  735. /* Only want to apply fix to reads */
  736. is_read = !(*((unsigned long*) SATA_DM_DBG1)
  737. & (port ? SATA_CORE_PORT1_DATA_DIR : SATA_CORE_PORT0_DATA_DIR));
  738. /* Check for an incomplete transfer, i.e. not a multiple of 512 bytes
  739. transferred (datacount_port register counts quads transferred) */
  740. quads_transferred = *((unsigned long*) (
  741. port ? SATA_DATACOUNT_PORT1 : SATA_DATACOUNT_PORT0));
  742. remainder = quads_transferred & 0x7f;
  743. sector_quads_remaining = remainder ? (0x80 - remainder) : 0;
  744. if (is_read && (sector_quads_remaining == 2)) {
  745. debug("SATA read fixup, only transfered %d quads, "
  746. "sector_quads_remaining %d, port %d\n",
  747. quads_transferred, sector_quads_remaining, port);
  748. int total_len = ATA_SECT_SIZE;
  749. ulong *sata_data_ptr = (void*) (
  750. port ? SATA_DATA_MUX_RAM1 : SATA_DATA_MUX_RAM0)
  751. + ((total_len - 8) % 2048);
  752. *candidate = *sata_data_ptr;
  753. *(candidate + 1) = *(sata_data_ptr + 1);
  754. }
  755. }
  756. void ide_input_data(int device, ulong *sect_buf, int words)
  757. {
  758. /* Only permit accesses to disks found to be present during ide_preinit() */
  759. if (!disk_present[device]) {
  760. return;
  761. }
  762. /* Select the required internal SATA drive */
  763. device_select(device);
  764. /* Start the DMA channel receiving data from the SATA core into the passed buffer */
  765. dma_start_read(sect_buf, words << 2);
  766. /* Sata core should finish before DMA */
  767. if (wait_not_busy(device, 30)) {
  768. printf("Timed out of wait for SATA device %d to have BUSY clear\n",
  769. device);
  770. }
  771. if (!wait_no_error(device)) {
  772. printf("oxnas_sata_output_data() Wait for ATA no-error timed-out\n");
  773. }
  774. /* Wait for DMA to finish */
  775. if (!wait_dma_not_busy(device)) {
  776. printf("Timed out of wait for DMA channel for SATA device %d to have in-progress clear\n",
  777. device);
  778. }
  779. if (words == ATA_SECTORWORDS)
  780. sata_bug_6320_workaround(device, sect_buf + words - 2);
  781. }
  782. static u32 scr_read(int device, unsigned int sc_reg)
  783. {
  784. /* Setup adr of required register. std regs start eight into async region */
  785. *(sata_regs_base[device] + SATA_LINK_RD_ADDR) = sc_reg
  786. * 4+ SATA_STD_ASYNC_REGS_OFF;
  787. /* Wait for data to be available */
  788. int loops = MAX_SRC_READ_LOOPS;
  789. do {
  790. if (*(sata_regs_base[device] + SATA_LINK_CONTROL) & 1UL) {
  791. break;
  792. }
  793. udelay(10);
  794. } while (--loops);
  795. if (!loops) {
  796. printf("scr_read() Timed out of wait for read completion\n");
  797. }
  798. /* Read the data from the async register */
  799. return *(sata_regs_base[device] + SATA_LINK_DATA);
  800. }
  801. static void scr_write(int device, unsigned int sc_reg, u32 val)
  802. {
  803. /* Setup the data for the write */
  804. *(sata_regs_base[device] + SATA_LINK_DATA) = val;
  805. /* Setup adr of required register. std regs start eight into async region */
  806. *(sata_regs_base[device] + SATA_LINK_WR_ADDR) = sc_reg
  807. * 4+ SATA_STD_ASYNC_REGS_OFF;
  808. /* Wait for data to be written */
  809. int loops = MAX_SRC_WRITE_LOOPS;
  810. do {
  811. if (*(sata_regs_base[device] + SATA_LINK_CONTROL) & 1UL) {
  812. break;
  813. }
  814. udelay(10);
  815. } while (--loops);
  816. if (!loops) {
  817. printf("scr_write() Timed out of wait for write completion\n");
  818. }
  819. }
  820. extern void workaround5458(void);
  821. #define PHY_LOOP_COUNT 25 /* Wait for upto 5 seconds for PHY to be found */
  822. #define LOS_AND_TX_LVL 0x2988
  823. #define TX_ATTEN 0x55629
  824. static int phy_reset(int device)
  825. {
  826. int phy_status = 0;
  827. int loops = 0;
  828. scr_write(device, (0x60 - SATA_STD_ASYNC_REGS_OFF) / 4, LOS_AND_TX_LVL);
  829. scr_write(device, (0x70 - SATA_STD_ASYNC_REGS_OFF) / 4, TX_ATTEN);
  830. /* limit it to Gen-1 SATA (1.5G) */
  831. scr_write(device, SATA_SCR_CONTROL, 0x311); /* Issue phy wake & core reset */
  832. scr_read(device, SATA_SCR_STATUS); /* Dummy read; flush */
  833. udelay(1000);
  834. scr_write(device, SATA_SCR_CONTROL, 0x310); /* Issue phy wake & clear core reset */
  835. /* Wait for upto 5 seconds for PHY to become ready */
  836. do {
  837. udelay(200000);
  838. if ((scr_read(device, SATA_SCR_STATUS) & 0xf) == 3) {
  839. scr_write(device, SATA_SCR_ERROR, ~0);
  840. phy_status = 1;
  841. break;
  842. }
  843. //printf("No SATA PHY found status:0x%x\n", scr_read(device, SATA_SCR_STATUS));
  844. } while (++loops < PHY_LOOP_COUNT);
  845. if (phy_status) {
  846. udelay(500000); /* wait half a second */
  847. }
  848. return phy_status;
  849. }
  850. #define FIS_LOOP_COUNT 25 /* Wait for upto 5 seconds for FIS to be received */
  851. static int wait_FIS(int device)
  852. {
  853. int status = 0;
  854. int loops = 0;
  855. do {
  856. udelay(200000);
  857. if (ide_inb(device, ATA_PORT_NSECT) > 0) {
  858. status = 1;
  859. break;
  860. }
  861. } while (++loops < FIS_LOOP_COUNT);
  862. return status;
  863. }
  864. #define SATA_PHY_ASIC_STAT (0x44900000)
  865. #define SATA_PHY_ASIC_DATA (0x44900004)
  866. /**
  867. * initialise functions and macros for ASIC implementation
  868. */
  869. #define PH_GAIN 2
  870. #define FR_GAIN 3
  871. #define PH_GAIN_OFFSET 6
  872. #define FR_GAIN_OFFSET 8
  873. #define PH_GAIN_MASK (0x3 << PH_GAIN_OFFSET)
  874. #define FR_GAIN_MASK (0x3 << FR_GAIN_OFFSET)
  875. #define USE_INT_SETTING (1<<5)
  876. #define CR_READ_ENABLE (1<<16)
  877. #define CR_WRITE_ENABLE (1<<17)
  878. #define CR_CAP_DATA (1<<18)
  879. static void wait_cr_ack(void)
  880. {
  881. while ((readl(SATA_PHY_ASIC_STAT) >> 16) & 0x1f)
  882. /* wait for an ack bit to be set */;
  883. }
  884. static unsigned short read_cr(unsigned short address)
  885. {
  886. writel(address, SATA_PHY_ASIC_STAT);
  887. wait_cr_ack();
  888. writel(CR_READ_ENABLE, SATA_PHY_ASIC_DATA);
  889. wait_cr_ack();
  890. return readl(SATA_PHY_ASIC_STAT);
  891. }
  892. static void write_cr(unsigned short data, unsigned short address)
  893. {
  894. writel(address, SATA_PHY_ASIC_STAT);
  895. wait_cr_ack();
  896. writel((data | CR_CAP_DATA), SATA_PHY_ASIC_DATA);
  897. wait_cr_ack();
  898. writel(CR_WRITE_ENABLE, SATA_PHY_ASIC_DATA);
  899. wait_cr_ack();
  900. return;
  901. }
  902. void workaround5458(void)
  903. {
  904. unsigned i;
  905. for (i = 0; i < 2; i++) {
  906. unsigned short rx_control = read_cr(0x201d + (i << 8));
  907. rx_control &= ~(PH_GAIN_MASK | FR_GAIN_MASK);
  908. rx_control |= PH_GAIN << PH_GAIN_OFFSET;
  909. rx_control |= FR_GAIN << FR_GAIN_OFFSET;
  910. rx_control |= USE_INT_SETTING;
  911. write_cr(rx_control, 0x201d + (i << 8));
  912. }
  913. }
  914. int ide_preinit(void)
  915. {
  916. int num_disks_found = 0;
  917. /* Initialise records of which disks are present to all present */
  918. int i;
  919. for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
  920. disk_present[i] = 1;
  921. }
  922. /* Block reset SATA and DMA cores */
  923. reset_block(SYS_CTRL_RST_SATA, 1);
  924. reset_block(SYS_CTRL_RST_SATA_LINK, 1);
  925. reset_block(SYS_CTRL_RST_SATA_PHY, 1);
  926. reset_block(SYS_CTRL_RST_SGDMA, 1);
  927. /* Enable clocks to SATA and DMA cores */
  928. enable_clock(SYS_CTRL_CLK_SATA);
  929. enable_clock(SYS_CTRL_CLK_DMA);
  930. udelay(5000);
  931. reset_block(SYS_CTRL_RST_SATA_PHY, 0);
  932. udelay(50);
  933. reset_block(SYS_CTRL_RST_SATA, 0);
  934. reset_block(SYS_CTRL_RST_SATA_LINK, 0);
  935. udelay(50);
  936. reset_block(SYS_CTRL_RST_SGDMA, 0);
  937. udelay(100);
  938. /* Apply the Synopsis SATA PHY workarounds */
  939. workaround5458();
  940. udelay(10000);
  941. /* disable and clear core interrupts */
  942. *((unsigned long*) SATA_HOST_REGS_BASE + SATA_INT_ENABLE_CLR_OFF) =
  943. ~0UL;
  944. *((unsigned long*) SATA_HOST_REGS_BASE + SATA_INT_CLR_OFF) = ~0UL;
  945. int device;
  946. for (device = 0; device < CONFIG_SYS_IDE_MAXDEVICE; device++) {
  947. int found = 0;
  948. int retries = 1;
  949. /* Disable SATA interrupts */
  950. *(sata_regs_base[device] + SATA_INT_ENABLE_CLR_OFF) = ~0UL;
  951. /* Clear any pending SATA interrupts */
  952. *(sata_regs_base[device] + SATA_INT_CLR_OFF) = ~0UL;
  953. do {
  954. /* clear sector count register for FIS detection */
  955. ide_outb(device, ATA_PORT_NSECT, 0);
  956. /* Get the PHY working */
  957. if (!phy_reset(device)) {
  958. printf("SATA PHY not ready for device %d\n",
  959. device);
  960. break;
  961. }
  962. if (!wait_FIS(device)) {
  963. printf("No FIS received from device %d\n",
  964. device);
  965. } else {
  966. if ((scr_read(device, SATA_SCR_STATUS) & 0xf)
  967. == 0x3) {
  968. if (wait_not_busy(device, 30)) {
  969. printf("Timed out of wait for SATA device %d to have BUSY clear\n",
  970. device);
  971. } else {
  972. ++num_disks_found;
  973. found = 1;
  974. }
  975. } else {
  976. printf("No SATA device %d found, PHY status = 0x%08x\n",
  977. device,
  978. scr_read(
  979. device,
  980. SATA_SCR_STATUS));
  981. }
  982. break;
  983. }
  984. } while (retries--);
  985. /* Record whether disk is present, so won't attempt to access it later */
  986. disk_present[device] = found;
  987. }
  988. /* post disk detection clean-up */
  989. for (device = 0; device < CONFIG_SYS_IDE_MAXDEVICE; device++) {
  990. if (disk_present[device]) {
  991. /* set as ata-5 (28-bit) */
  992. *(sata_regs_base[device] + SATA_DRIVE_CONTROL_OFF) =
  993. 0UL;
  994. /* clear phy/link errors */
  995. scr_write(device, SATA_SCR_ERROR, ~0);
  996. /* clear host errors */
  997. *(sata_regs_base[device] + SATA_CONTROL_OFF) |=
  998. SATA_SCTL_CLR_ERR;
  999. /* clear interrupt register as this clears the error bit in the IDE
  1000. status register */
  1001. *(sata_regs_base[device] + SATA_INT_CLR_OFF) = ~0UL;
  1002. }
  1003. }
  1004. return !num_disks_found;
  1005. }