spi-aspeed-smc.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ASPEED FMC/SPI Controller driver
  4. *
  5. * Copyright (c) 2022 ASPEED Corporation.
  6. * Copyright (c) 2022 IBM Corporation.
  7. *
  8. * Author:
  9. * Chin-Ting Kuo <chin-ting_kuo@aspeedtech.com>
  10. * Cedric Le Goater <clg@kaod.org>
  11. */
  12. #include <asm/io.h>
  13. #include <clk.h>
  14. #include <common.h>
  15. #include <dm.h>
  16. #include <dm/device_compat.h>
  17. #include <linux/bitops.h>
  18. #include <linux/bug.h>
  19. #include <linux/err.h>
  20. #include <linux/iopoll.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mtd/spi-nor.h>
  23. #include <linux/sizes.h>
  24. #include <malloc.h>
  25. #include <spi.h>
  26. #include <spi-mem.h>
  27. #define ASPEED_SPI_MAX_CS 5
  28. #define CTRL_IO_SINGLE_DATA 0
  29. #define CTRL_IO_QUAD_DATA BIT(30)
  30. #define CTRL_IO_DUAL_DATA BIT(29)
  31. #define CTRL_IO_MODE_USER GENMASK(1, 0)
  32. #define CTRL_IO_MODE_CMD_READ BIT(0)
  33. #define CTRL_IO_MODE_CMD_WRITE BIT(1)
  34. #define CTRL_STOP_ACTIVE BIT(2)
  35. struct aspeed_spi_regs {
  36. u32 conf; /* 0x00 CE Type Setting */
  37. u32 ctrl; /* 0x04 CE Control */
  38. u32 intr_ctrl; /* 0x08 Interrupt Control and Status */
  39. u32 cmd_ctrl; /* 0x0c Command Control */
  40. u32 ce_ctrl[ASPEED_SPI_MAX_CS]; /* 0x10 .. 0x20 CEx Control */
  41. u32 _reserved0[3]; /* .. */
  42. u32 segment_addr[ASPEED_SPI_MAX_CS]; /* 0x30 .. 0x40 Segment Address */
  43. u32 _reserved1[3]; /* .. */
  44. u32 soft_rst_cmd_ctrl; /* 0x50 Auto Soft-Reset Command Control */
  45. u32 _reserved2[11]; /* .. */
  46. u32 dma_ctrl; /* 0x80 DMA Control/Status */
  47. u32 dma_flash_addr; /* 0x84 DMA Flash Side Address */
  48. u32 dma_dram_addr; /* 0x88 DMA DRAM Side Address */
  49. u32 dma_len; /* 0x8c DMA Length Register */
  50. u32 dma_checksum; /* 0x90 Checksum Calculation Result */
  51. u32 timings[ASPEED_SPI_MAX_CS]; /* 0x94 Read Timing Compensation */
  52. };
  53. struct aspeed_spi_plat {
  54. u8 max_cs;
  55. void __iomem *ahb_base; /* AHB address base for all flash devices. */
  56. fdt_size_t ahb_sz; /* Overall AHB window size for all flash device. */
  57. u32 hclk_rate; /* AHB clock rate */
  58. };
  59. struct aspeed_spi_flash {
  60. void __iomem *ahb_base;
  61. u32 ahb_decoded_sz;
  62. u32 ce_ctrl_user;
  63. u32 ce_ctrl_read;
  64. u32 max_freq;
  65. };
  66. struct aspeed_spi_priv {
  67. u32 num_cs;
  68. struct aspeed_spi_regs *regs;
  69. struct aspeed_spi_info *info;
  70. struct aspeed_spi_flash flashes[ASPEED_SPI_MAX_CS];
  71. bool fixed_decoded_range;
  72. };
  73. struct aspeed_spi_info {
  74. u32 io_mode_mask;
  75. u32 max_bus_width;
  76. u32 min_decoded_sz;
  77. u32 clk_ctrl_mask;
  78. void (*set_4byte)(struct udevice *bus, u32 cs);
  79. u32 (*segment_start)(struct udevice *bus, u32 reg);
  80. u32 (*segment_end)(struct udevice *bus, u32 reg);
  81. u32 (*segment_reg)(u32 start, u32 end);
  82. int (*adjust_decoded_sz)(struct udevice *bus);
  83. u32 (*get_clk_setting)(struct udevice *dev, uint hz);
  84. };
  85. struct aspeed_spi_decoded_range {
  86. u32 cs;
  87. u32 ahb_base;
  88. u32 sz;
  89. };
  90. static const struct aspeed_spi_info ast2400_spi_info;
  91. static const struct aspeed_spi_info ast2500_fmc_info;
  92. static const struct aspeed_spi_info ast2500_spi_info;
  93. static int aspeed_spi_decoded_range_config(struct udevice *bus);
  94. static int aspeed_spi_trim_decoded_size(struct udevice *bus);
  95. static u32 aspeed_spi_get_io_mode(u32 bus_width)
  96. {
  97. switch (bus_width) {
  98. case 1:
  99. return CTRL_IO_SINGLE_DATA;
  100. case 2:
  101. return CTRL_IO_DUAL_DATA;
  102. case 4:
  103. return CTRL_IO_QUAD_DATA;
  104. default:
  105. /* keep in default value */
  106. return CTRL_IO_SINGLE_DATA;
  107. }
  108. }
  109. static u32 ast2400_spi_segment_start(struct udevice *bus, u32 reg)
  110. {
  111. struct aspeed_spi_plat *plat = dev_get_plat(bus);
  112. u32 start_offset = ((reg >> 16) & 0xff) << 23;
  113. if (start_offset == 0)
  114. return (u32)plat->ahb_base;
  115. return (u32)plat->ahb_base + start_offset;
  116. }
  117. static u32 ast2400_spi_segment_end(struct udevice *bus, u32 reg)
  118. {
  119. struct aspeed_spi_plat *plat = dev_get_plat(bus);
  120. u32 end_offset = ((reg >> 24) & 0xff) << 23;
  121. /* Meaningless end_offset, set to physical ahb base. */
  122. if (end_offset == 0)
  123. return (u32)plat->ahb_base;
  124. return (u32)plat->ahb_base + end_offset;
  125. }
  126. static u32 ast2400_spi_segment_reg(u32 start, u32 end)
  127. {
  128. if (start == end)
  129. return 0;
  130. return ((((start) >> 23) & 0xff) << 16) | ((((end) >> 23) & 0xff) << 24);
  131. }
  132. static void ast2400_fmc_chip_set_4byte(struct udevice *bus, u32 cs)
  133. {
  134. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  135. u32 reg_val;
  136. reg_val = readl(&priv->regs->ctrl);
  137. reg_val |= 0x1 << cs;
  138. writel(reg_val, &priv->regs->ctrl);
  139. }
  140. static void ast2400_spi_chip_set_4byte(struct udevice *bus, u32 cs)
  141. {
  142. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  143. struct aspeed_spi_flash *flash = &priv->flashes[cs];
  144. flash->ce_ctrl_read |= BIT(13);
  145. writel(flash->ce_ctrl_read, &priv->regs->ctrl);
  146. }
  147. /* Transfer maximum clock frequency to register setting */
  148. static u32 ast2400_get_clk_setting(struct udevice *dev, uint max_hz)
  149. {
  150. struct aspeed_spi_plat *plat = dev_get_plat(dev->parent);
  151. struct aspeed_spi_priv *priv = dev_get_priv(dev->parent);
  152. struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
  153. u32 hclk_clk = plat->hclk_rate;
  154. u32 hclk_div = 0x0000; /* default value */
  155. u32 i;
  156. bool found = false;
  157. /* HCLK/1 .. HCLK/16 */
  158. u32 hclk_masks[] = {15, 7, 14, 6, 13, 5, 12, 4,
  159. 11, 3, 10, 2, 9, 1, 8, 0};
  160. /* FMC/SPIR10[11:8] */
  161. for (i = 0; i < ARRAY_SIZE(hclk_masks); i++) {
  162. if (hclk_clk / (i + 1) <= max_hz) {
  163. found = true;
  164. break;
  165. }
  166. }
  167. if (found) {
  168. hclk_div = hclk_masks[i] << 8;
  169. priv->flashes[slave_plat->cs].max_freq = hclk_clk / (i + 1);
  170. }
  171. dev_dbg(dev, "found: %s, hclk: %d, max_clk: %d\n", found ? "yes" : "no",
  172. hclk_clk, max_hz);
  173. if (found) {
  174. dev_dbg(dev, "h_div: %d (mask %x), speed: %d\n",
  175. i + 1, hclk_masks[i], priv->flashes[slave_plat->cs].max_freq);
  176. }
  177. return hclk_div;
  178. }
  179. static u32 ast2500_spi_segment_start(struct udevice *bus, u32 reg)
  180. {
  181. struct aspeed_spi_plat *plat = dev_get_plat(bus);
  182. u32 start_offset = ((reg >> 16) & 0xff) << 23;
  183. if (start_offset == 0)
  184. return (u32)plat->ahb_base;
  185. return (u32)plat->ahb_base + start_offset;
  186. }
  187. static u32 ast2500_spi_segment_end(struct udevice *bus, u32 reg)
  188. {
  189. struct aspeed_spi_plat *plat = dev_get_plat(bus);
  190. u32 end_offset = ((reg >> 24) & 0xff) << 23;
  191. /* Meaningless end_offset, set to physical ahb base. */
  192. if (end_offset == 0)
  193. return (u32)plat->ahb_base;
  194. return (u32)plat->ahb_base + end_offset;
  195. }
  196. static u32 ast2500_spi_segment_reg(u32 start, u32 end)
  197. {
  198. if (start == end)
  199. return 0;
  200. return ((((start) >> 23) & 0xff) << 16) | ((((end) >> 23) & 0xff) << 24);
  201. }
  202. static void ast2500_spi_chip_set_4byte(struct udevice *bus, u32 cs)
  203. {
  204. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  205. u32 reg_val;
  206. reg_val = readl(&priv->regs->ctrl);
  207. reg_val |= 0x1 << cs;
  208. writel(reg_val, &priv->regs->ctrl);
  209. }
  210. /*
  211. * For AST2500, the minimum address decoded size for each CS
  212. * is 8MB instead of zero. This address decoded size is
  213. * mandatory for each CS no matter whether it will be used.
  214. * This is a HW limitation.
  215. */
  216. static int ast2500_adjust_decoded_size(struct udevice *bus)
  217. {
  218. struct aspeed_spi_plat *plat = dev_get_plat(bus);
  219. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  220. struct aspeed_spi_flash *flashes = &priv->flashes[0];
  221. int ret;
  222. int i;
  223. int cs;
  224. u32 pre_sz;
  225. u32 lack_sz;
  226. /* Assign min_decoded_sz to unused CS. */
  227. for (cs = priv->num_cs; cs < plat->max_cs; cs++)
  228. flashes[cs].ahb_decoded_sz = priv->info->min_decoded_sz;
  229. /*
  230. * If command mode or normal mode is used, the start address of a
  231. * decoded range should be multiple of its related flash size.
  232. * Namely, the total decoded size from flash 0 to flash N should
  233. * be multiple of the size of flash (N + 1).
  234. */
  235. for (cs = priv->num_cs - 1; cs >= 0; cs--) {
  236. pre_sz = 0;
  237. for (i = 0; i < cs; i++)
  238. pre_sz += flashes[i].ahb_decoded_sz;
  239. if (flashes[cs].ahb_decoded_sz != 0 &&
  240. (pre_sz % flashes[cs].ahb_decoded_sz) != 0) {
  241. lack_sz = flashes[cs].ahb_decoded_sz -
  242. (pre_sz % flashes[cs].ahb_decoded_sz);
  243. flashes[0].ahb_decoded_sz += lack_sz;
  244. }
  245. }
  246. ret = aspeed_spi_trim_decoded_size(bus);
  247. if (ret != 0)
  248. return ret;
  249. return 0;
  250. }
  251. static u32 ast2500_get_clk_setting(struct udevice *dev, uint max_hz)
  252. {
  253. struct aspeed_spi_plat *plat = dev_get_plat(dev->parent);
  254. struct aspeed_spi_priv *priv = dev_get_priv(dev->parent);
  255. struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
  256. u32 hclk_clk = plat->hclk_rate;
  257. u32 hclk_div = 0x0000; /* default value */
  258. u32 i;
  259. bool found = false;
  260. /* HCLK/1 .. HCLK/16 */
  261. u32 hclk_masks[] = {15, 7, 14, 6, 13, 5, 12, 4,
  262. 11, 3, 10, 2, 9, 1, 8, 0};
  263. /* FMC/SPIR10[11:8] */
  264. for (i = 0; i < ARRAY_SIZE(hclk_masks); i++) {
  265. if (hclk_clk / (i + 1) <= max_hz) {
  266. found = true;
  267. priv->flashes[slave_plat->cs].max_freq =
  268. hclk_clk / (i + 1);
  269. break;
  270. }
  271. }
  272. if (found) {
  273. hclk_div = hclk_masks[i] << 8;
  274. goto end;
  275. }
  276. for (i = 0; i < ARRAY_SIZE(hclk_masks); i++) {
  277. if (hclk_clk / ((i + 1) * 4) <= max_hz) {
  278. found = true;
  279. priv->flashes[slave_plat->cs].max_freq =
  280. hclk_clk / ((i + 1) * 4);
  281. break;
  282. }
  283. }
  284. if (found)
  285. hclk_div = BIT(13) | (hclk_masks[i] << 8);
  286. end:
  287. dev_dbg(dev, "found: %s, hclk: %d, max_clk: %d\n", found ? "yes" : "no",
  288. hclk_clk, max_hz);
  289. if (found) {
  290. dev_dbg(dev, "h_div: %d (mask %x), speed: %d\n",
  291. i + 1, hclk_masks[i], priv->flashes[slave_plat->cs].max_freq);
  292. }
  293. return hclk_div;
  294. }
  295. static u32 ast2600_spi_segment_start(struct udevice *bus, u32 reg)
  296. {
  297. struct aspeed_spi_plat *plat = dev_get_plat(bus);
  298. u32 start_offset = (reg << 16) & 0x0ff00000;
  299. if (start_offset == 0)
  300. return (u32)plat->ahb_base;
  301. return (u32)plat->ahb_base + start_offset;
  302. }
  303. static u32 ast2600_spi_segment_end(struct udevice *bus, u32 reg)
  304. {
  305. struct aspeed_spi_plat *plat = dev_get_plat(bus);
  306. u32 end_offset = reg & 0x0ff00000;
  307. /* Meaningless end_offset, set to physical ahb base. */
  308. if (end_offset == 0)
  309. return (u32)plat->ahb_base;
  310. return (u32)plat->ahb_base + end_offset + 0x100000;
  311. }
  312. static u32 ast2600_spi_segment_reg(u32 start, u32 end)
  313. {
  314. if (start == end)
  315. return 0;
  316. return ((start & 0x0ff00000) >> 16) | ((end - 0x100000) & 0x0ff00000);
  317. }
  318. static void ast2600_spi_chip_set_4byte(struct udevice *bus, u32 cs)
  319. {
  320. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  321. u32 reg_val;
  322. reg_val = readl(&priv->regs->ctrl);
  323. reg_val |= 0x11 << cs;
  324. writel(reg_val, &priv->regs->ctrl);
  325. }
  326. static int ast2600_adjust_decoded_size(struct udevice *bus)
  327. {
  328. struct aspeed_spi_plat *plat = dev_get_plat(bus);
  329. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  330. struct aspeed_spi_flash *flashes = &priv->flashes[0];
  331. int ret;
  332. int i;
  333. int cs;
  334. u32 pre_sz;
  335. u32 lack_sz;
  336. /* Close unused CS. */
  337. for (cs = priv->num_cs; cs < plat->max_cs; cs++)
  338. flashes[cs].ahb_decoded_sz = 0;
  339. /*
  340. * If command mode or normal mode is used, the start address of a
  341. * decoded range should be multiple of its related flash size.
  342. * Namely, the total decoded size from flash 0 to flash N should
  343. * be multiple of the size of flash (N + 1).
  344. */
  345. for (cs = priv->num_cs - 1; cs >= 0; cs--) {
  346. pre_sz = 0;
  347. for (i = 0; i < cs; i++)
  348. pre_sz += flashes[i].ahb_decoded_sz;
  349. if (flashes[cs].ahb_decoded_sz != 0 &&
  350. (pre_sz % flashes[cs].ahb_decoded_sz) != 0) {
  351. lack_sz = flashes[cs].ahb_decoded_sz -
  352. (pre_sz % flashes[cs].ahb_decoded_sz);
  353. flashes[0].ahb_decoded_sz += lack_sz;
  354. }
  355. }
  356. ret = aspeed_spi_trim_decoded_size(bus);
  357. if (ret != 0)
  358. return ret;
  359. return 0;
  360. }
  361. static u32 ast2600_get_clk_setting(struct udevice *dev, uint max_hz)
  362. {
  363. struct aspeed_spi_plat *plat = dev_get_plat(dev->parent);
  364. struct aspeed_spi_priv *priv = dev_get_priv(dev->parent);
  365. struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
  366. u32 hclk_clk = plat->hclk_rate;
  367. u32 hclk_div = 0x0400; /* default value */
  368. u32 i, j;
  369. bool found = false;
  370. /* HCLK/1 .. HCLK/16 */
  371. u32 hclk_masks[] = {15, 7, 14, 6, 13, 5, 12, 4,
  372. 11, 3, 10, 2, 9, 1, 8, 0};
  373. /* FMC/SPIR10[27:24] */
  374. for (j = 0; j < 0xf; j++) {
  375. /* FMC/SPIR10[11:8] */
  376. for (i = 0; i < ARRAY_SIZE(hclk_masks); i++) {
  377. if (i == 0 && j == 0)
  378. continue;
  379. if (hclk_clk / (i + 1 + (j * 16)) <= max_hz) {
  380. found = true;
  381. break;
  382. }
  383. }
  384. if (found) {
  385. hclk_div = ((j << 24) | hclk_masks[i] << 8);
  386. priv->flashes[slave_plat->cs].max_freq =
  387. hclk_clk / (i + 1 + j * 16);
  388. break;
  389. }
  390. }
  391. dev_dbg(dev, "found: %s, hclk: %d, max_clk: %d\n", found ? "yes" : "no",
  392. hclk_clk, max_hz);
  393. if (found) {
  394. dev_dbg(dev, "base_clk: %d, h_div: %d (mask %x), speed: %d\n",
  395. j, i + 1, hclk_masks[i], priv->flashes[slave_plat->cs].max_freq);
  396. }
  397. return hclk_div;
  398. }
  399. /*
  400. * As the flash size grows up, we need to trim some decoded
  401. * size if needed for the sake of conforming the maximum
  402. * decoded size. We trim the decoded size from the largest
  403. * CS in order to avoid affecting the default boot up sequence
  404. * from CS0 where command mode or normal mode is used.
  405. * Notice, if a CS decoded size is trimmed, command mode may
  406. * not work perfectly on that CS.
  407. */
  408. static int aspeed_spi_trim_decoded_size(struct udevice *bus)
  409. {
  410. struct aspeed_spi_plat *plat = dev_get_plat(bus);
  411. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  412. struct aspeed_spi_flash *flashes = &priv->flashes[0];
  413. u32 total_sz;
  414. int cs = plat->max_cs - 1;
  415. u32 i;
  416. do {
  417. total_sz = 0;
  418. for (i = 0; i < plat->max_cs; i++)
  419. total_sz += flashes[i].ahb_decoded_sz;
  420. if (flashes[cs].ahb_decoded_sz <= priv->info->min_decoded_sz)
  421. cs--;
  422. if (cs < 0)
  423. return -ENOMEM;
  424. if (total_sz > plat->ahb_sz) {
  425. flashes[cs].ahb_decoded_sz -=
  426. priv->info->min_decoded_sz;
  427. total_sz -= priv->info->min_decoded_sz;
  428. }
  429. } while (total_sz > plat->ahb_sz);
  430. return 0;
  431. }
  432. static int aspeed_spi_read_from_ahb(void __iomem *ahb_base, void *buf,
  433. size_t len)
  434. {
  435. size_t offset = 0;
  436. if (IS_ALIGNED((uintptr_t)ahb_base, sizeof(uintptr_t)) &&
  437. IS_ALIGNED((uintptr_t)buf, sizeof(uintptr_t))) {
  438. readsl(ahb_base, buf, len >> 2);
  439. offset = len & ~0x3;
  440. len -= offset;
  441. }
  442. readsb(ahb_base, (u8 *)buf + offset, len);
  443. return 0;
  444. }
  445. static int aspeed_spi_write_to_ahb(void __iomem *ahb_base, const void *buf,
  446. size_t len)
  447. {
  448. size_t offset = 0;
  449. if (IS_ALIGNED((uintptr_t)ahb_base, sizeof(uintptr_t)) &&
  450. IS_ALIGNED((uintptr_t)buf, sizeof(uintptr_t))) {
  451. writesl(ahb_base, buf, len >> 2);
  452. offset = len & ~0x3;
  453. len -= offset;
  454. }
  455. writesb(ahb_base, (u8 *)buf + offset, len);
  456. return 0;
  457. }
  458. /*
  459. * Currently, only support 1-1-1, 1-1-2 or 1-1-4
  460. * SPI NOR flash operation format.
  461. */
  462. static bool aspeed_spi_supports_op(struct spi_slave *slave,
  463. const struct spi_mem_op *op)
  464. {
  465. struct udevice *bus = slave->dev->parent;
  466. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  467. if (op->cmd.buswidth > 1)
  468. return false;
  469. if (op->addr.nbytes != 0) {
  470. if (op->addr.buswidth > 1)
  471. return false;
  472. if (op->addr.nbytes < 3 || op->addr.nbytes > 4)
  473. return false;
  474. }
  475. if (op->dummy.nbytes != 0) {
  476. if (op->dummy.buswidth > 1 || op->dummy.nbytes > 7)
  477. return false;
  478. }
  479. if (op->data.nbytes != 0 &&
  480. op->data.buswidth > priv->info->max_bus_width)
  481. return false;
  482. if (!spi_mem_default_supports_op(slave, op))
  483. return false;
  484. return true;
  485. }
  486. static int aspeed_spi_exec_op_user_mode(struct spi_slave *slave,
  487. const struct spi_mem_op *op)
  488. {
  489. struct udevice *dev = slave->dev;
  490. struct udevice *bus = dev->parent;
  491. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  492. struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(slave->dev);
  493. u32 cs = slave_plat->cs;
  494. u32 ce_ctrl_reg = (u32)&priv->regs->ce_ctrl[cs];
  495. u32 ce_ctrl_val;
  496. struct aspeed_spi_flash *flash = &priv->flashes[cs];
  497. u8 dummy_data[16] = {0};
  498. u8 addr[4] = {0};
  499. int i;
  500. dev_dbg(dev, "cmd:%x(%d),addr:%llx(%d),dummy:%d(%d),data_len:0x%x(%d)\n",
  501. op->cmd.opcode, op->cmd.buswidth, op->addr.val,
  502. op->addr.buswidth, op->dummy.nbytes, op->dummy.buswidth,
  503. op->data.nbytes, op->data.buswidth);
  504. if (priv->info == &ast2400_spi_info)
  505. ce_ctrl_reg = (u32)&priv->regs->ctrl;
  506. /*
  507. * Set controller to 4-byte address mode
  508. * if flash is in 4-byte address mode.
  509. */
  510. if (op->cmd.opcode == SPINOR_OP_EN4B)
  511. priv->info->set_4byte(bus, cs);
  512. /* Start user mode */
  513. ce_ctrl_val = flash->ce_ctrl_user;
  514. writel(ce_ctrl_val, ce_ctrl_reg);
  515. ce_ctrl_val &= (~CTRL_STOP_ACTIVE);
  516. writel(ce_ctrl_val, ce_ctrl_reg);
  517. /* Send command */
  518. aspeed_spi_write_to_ahb(flash->ahb_base, &op->cmd.opcode, 1);
  519. /* Send address */
  520. for (i = op->addr.nbytes; i > 0; i--) {
  521. addr[op->addr.nbytes - i] =
  522. ((u32)op->addr.val >> ((i - 1) * 8)) & 0xff;
  523. }
  524. /* Change io_mode */
  525. ce_ctrl_val &= ~priv->info->io_mode_mask;
  526. ce_ctrl_val |= aspeed_spi_get_io_mode(op->addr.buswidth);
  527. writel(ce_ctrl_val, ce_ctrl_reg);
  528. aspeed_spi_write_to_ahb(flash->ahb_base, addr, op->addr.nbytes);
  529. /* Send dummy cycles */
  530. aspeed_spi_write_to_ahb(flash->ahb_base, dummy_data, op->dummy.nbytes);
  531. /* Change io_mode */
  532. ce_ctrl_val &= ~priv->info->io_mode_mask;
  533. ce_ctrl_val |= aspeed_spi_get_io_mode(op->data.buswidth);
  534. writel(ce_ctrl_val, ce_ctrl_reg);
  535. /* Send data */
  536. if (op->data.dir == SPI_MEM_DATA_OUT) {
  537. aspeed_spi_write_to_ahb(flash->ahb_base, op->data.buf.out,
  538. op->data.nbytes);
  539. } else {
  540. aspeed_spi_read_from_ahb(flash->ahb_base, op->data.buf.in,
  541. op->data.nbytes);
  542. }
  543. ce_ctrl_val |= CTRL_STOP_ACTIVE;
  544. writel(ce_ctrl_val, ce_ctrl_reg);
  545. /* Restore controller setting. */
  546. writel(flash->ce_ctrl_read, ce_ctrl_reg);
  547. return 0;
  548. }
  549. static int aspeed_spi_dirmap_create(struct spi_mem_dirmap_desc *desc)
  550. {
  551. int ret = 0;
  552. struct udevice *dev = desc->slave->dev;
  553. struct udevice *bus = dev->parent;
  554. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  555. struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
  556. const struct aspeed_spi_info *info = priv->info;
  557. struct spi_mem_op op_tmpl = desc->info.op_tmpl;
  558. u32 i;
  559. u32 cs = slave_plat->cs;
  560. u32 cmd_io_conf;
  561. u32 ce_ctrl_reg;
  562. if (desc->info.op_tmpl.data.dir == SPI_MEM_DATA_OUT) {
  563. /*
  564. * dirmap_write is not supported currently due to a HW
  565. * limitation for command write mode: The written data
  566. * length should be multiple of 4-byte.
  567. */
  568. return -EOPNOTSUPP;
  569. }
  570. ce_ctrl_reg = (u32)&priv->regs->ce_ctrl[cs];
  571. if (info == &ast2400_spi_info)
  572. ce_ctrl_reg = (u32)&priv->regs->ctrl;
  573. if (desc->info.length > 0x1000000)
  574. priv->info->set_4byte(bus, cs);
  575. /* AST2400 SPI1 doesn't have decoded address segment register. */
  576. if (info != &ast2400_spi_info) {
  577. priv->flashes[cs].ahb_decoded_sz = desc->info.length;
  578. for (i = 0; i < priv->num_cs; i++) {
  579. dev_dbg(dev, "cs: %d, sz: 0x%x\n", i,
  580. priv->flashes[cs].ahb_decoded_sz);
  581. }
  582. ret = aspeed_spi_decoded_range_config(bus);
  583. if (ret)
  584. return ret;
  585. }
  586. cmd_io_conf = aspeed_spi_get_io_mode(op_tmpl.data.buswidth) |
  587. op_tmpl.cmd.opcode << 16 |
  588. ((op_tmpl.dummy.nbytes) & 0x3) << 6 |
  589. ((op_tmpl.dummy.nbytes) & 0x4) << 14 |
  590. CTRL_IO_MODE_CMD_READ;
  591. priv->flashes[cs].ce_ctrl_read &= priv->info->clk_ctrl_mask;
  592. priv->flashes[cs].ce_ctrl_read |= cmd_io_conf;
  593. writel(priv->flashes[cs].ce_ctrl_read, ce_ctrl_reg);
  594. dev_dbg(dev, "read bus width: %d ce_ctrl_val: 0x%08x\n",
  595. op_tmpl.data.buswidth, priv->flashes[cs].ce_ctrl_read);
  596. return ret;
  597. }
  598. static ssize_t aspeed_spi_dirmap_read(struct spi_mem_dirmap_desc *desc,
  599. u64 offs, size_t len, void *buf)
  600. {
  601. struct udevice *dev = desc->slave->dev;
  602. struct aspeed_spi_priv *priv = dev_get_priv(dev->parent);
  603. struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
  604. u32 cs = slave_plat->cs;
  605. int ret;
  606. dev_dbg(dev, "read op:0x%x, addr:0x%llx, len:0x%x\n",
  607. desc->info.op_tmpl.cmd.opcode, offs, len);
  608. if (priv->flashes[cs].ahb_decoded_sz < offs + len ||
  609. (offs % 4) != 0) {
  610. ret = aspeed_spi_exec_op_user_mode(desc->slave,
  611. &desc->info.op_tmpl);
  612. if (ret != 0)
  613. return 0;
  614. } else {
  615. memcpy_fromio(buf, priv->flashes[cs].ahb_base + offs, len);
  616. }
  617. return len;
  618. }
  619. static struct aspeed_spi_flash *aspeed_spi_get_flash(struct udevice *dev)
  620. {
  621. struct udevice *bus = dev->parent;
  622. struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
  623. struct aspeed_spi_plat *plat = dev_get_plat(bus);
  624. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  625. u32 cs = slave_plat->cs;
  626. if (cs >= plat->max_cs) {
  627. dev_err(dev, "invalid CS %u\n", cs);
  628. return NULL;
  629. }
  630. return &priv->flashes[cs];
  631. }
  632. static void aspeed_spi_decoded_base_calculate(struct udevice *bus)
  633. {
  634. struct aspeed_spi_plat *plat = dev_get_plat(bus);
  635. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  636. u32 cs;
  637. if (priv->fixed_decoded_range)
  638. return;
  639. priv->flashes[0].ahb_base = plat->ahb_base;
  640. for (cs = 1; cs < plat->max_cs; cs++) {
  641. priv->flashes[cs].ahb_base =
  642. priv->flashes[cs - 1].ahb_base +
  643. priv->flashes[cs - 1].ahb_decoded_sz;
  644. }
  645. }
  646. static void aspeed_spi_decoded_range_set(struct udevice *bus)
  647. {
  648. struct aspeed_spi_plat *plat = dev_get_plat(bus);
  649. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  650. u32 decoded_reg_val;
  651. u32 start_addr, end_addr;
  652. u32 cs;
  653. for (cs = 0; cs < plat->max_cs; cs++) {
  654. start_addr = (u32)priv->flashes[cs].ahb_base;
  655. end_addr = (u32)priv->flashes[cs].ahb_base +
  656. priv->flashes[cs].ahb_decoded_sz;
  657. decoded_reg_val = priv->info->segment_reg(start_addr, end_addr);
  658. writel(decoded_reg_val, &priv->regs->segment_addr[cs]);
  659. dev_dbg(bus, "cs: %d, decoded_reg: 0x%x, start: 0x%x, end: 0x%x\n",
  660. cs, decoded_reg_val, start_addr, end_addr);
  661. }
  662. }
  663. static int aspeed_spi_decoded_range_config(struct udevice *bus)
  664. {
  665. int ret = 0;
  666. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  667. if (priv->info->adjust_decoded_sz &&
  668. !priv->fixed_decoded_range) {
  669. ret = priv->info->adjust_decoded_sz(bus);
  670. if (ret != 0)
  671. return ret;
  672. }
  673. aspeed_spi_decoded_base_calculate(bus);
  674. aspeed_spi_decoded_range_set(bus);
  675. return ret;
  676. }
  677. static int aspeed_spi_decoded_ranges_sanity(struct udevice *bus)
  678. {
  679. struct aspeed_spi_plat *plat = dev_get_plat(bus);
  680. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  681. u32 cs;
  682. u32 total_sz = 0;
  683. /* Check overall size. */
  684. for (cs = 0; cs < plat->max_cs; cs++)
  685. total_sz += priv->flashes[cs].ahb_decoded_sz;
  686. if (total_sz > plat->ahb_sz) {
  687. dev_err(bus, "invalid total size 0x%08x\n", total_sz);
  688. return -EINVAL;
  689. }
  690. /* Check each decoded range size for AST2500. */
  691. if (priv->info == &ast2500_fmc_info ||
  692. priv->info == &ast2500_spi_info) {
  693. for (cs = 0; cs < plat->max_cs; cs++) {
  694. if (priv->flashes[cs].ahb_decoded_sz <
  695. priv->info->min_decoded_sz) {
  696. dev_err(bus, "insufficient decoded range.\n");
  697. return -EINVAL;
  698. }
  699. }
  700. }
  701. /*
  702. * Check overlay. Here, we assume the deccded ranges and
  703. * address base are monotonic increasing with CE#.
  704. */
  705. for (cs = plat->max_cs - 1; cs > 0; cs--) {
  706. if ((u32)priv->flashes[cs].ahb_base != 0 &&
  707. (u32)priv->flashes[cs].ahb_base <
  708. (u32)priv->flashes[cs - 1].ahb_base +
  709. priv->flashes[cs - 1].ahb_decoded_sz) {
  710. dev_err(bus, "decoded range overlay 0x%08x 0x%08x\n",
  711. (u32)priv->flashes[cs].ahb_base,
  712. (u32)priv->flashes[cs - 1].ahb_base);
  713. return -EINVAL;
  714. }
  715. }
  716. return 0;
  717. }
  718. static int aspeed_spi_read_fixed_decoded_ranges(struct udevice *bus)
  719. {
  720. int ret = 0;
  721. struct aspeed_spi_plat *plat = dev_get_plat(bus);
  722. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  723. const char *range_prop = "decoded-ranges";
  724. struct aspeed_spi_decoded_range ranges[ASPEED_SPI_MAX_CS];
  725. const struct property *prop;
  726. u32 prop_sz;
  727. u32 count;
  728. u32 i;
  729. priv->fixed_decoded_range = false;
  730. prop = dev_read_prop(bus, range_prop, &prop_sz);
  731. if (!prop)
  732. return 0;
  733. count = prop_sz / sizeof(struct aspeed_spi_decoded_range);
  734. if (count > plat->max_cs || count < priv->num_cs) {
  735. dev_err(bus, "invalid '%s' property %d %d\n",
  736. range_prop, count, priv->num_cs);
  737. return -EINVAL;
  738. }
  739. ret = dev_read_u32_array(bus, range_prop, (u32 *)ranges, count * 3);
  740. if (ret)
  741. return ret;
  742. for (i = 0; i < count; i++) {
  743. priv->flashes[ranges[i].cs].ahb_base =
  744. (void __iomem *)ranges[i].ahb_base;
  745. priv->flashes[ranges[i].cs].ahb_decoded_sz =
  746. ranges[i].sz;
  747. }
  748. for (i = 0; i < plat->max_cs; i++) {
  749. dev_dbg(bus, "ahb_base: 0x%p, size: 0x%08x\n",
  750. priv->flashes[i].ahb_base,
  751. priv->flashes[i].ahb_decoded_sz);
  752. }
  753. ret = aspeed_spi_decoded_ranges_sanity(bus);
  754. if (ret != 0)
  755. return ret;
  756. priv->fixed_decoded_range = true;
  757. return 0;
  758. }
  759. /*
  760. * Initialize SPI controller for each chip select.
  761. * Here, only the minimum decode range is configured
  762. * in order to get device (SPI NOR flash) information
  763. * at the early stage.
  764. */
  765. static int aspeed_spi_ctrl_init(struct udevice *bus)
  766. {
  767. int ret;
  768. struct aspeed_spi_plat *plat = dev_get_plat(bus);
  769. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  770. u32 cs;
  771. u32 reg_val;
  772. u32 decoded_sz;
  773. /* Enable write capability for all CS. */
  774. reg_val = readl(&priv->regs->conf);
  775. if (priv->info == &ast2400_spi_info) {
  776. writel(reg_val | BIT(0), &priv->regs->conf);
  777. } else {
  778. writel(reg_val | (GENMASK(plat->max_cs - 1, 0) << 16),
  779. &priv->regs->conf);
  780. }
  781. memset(priv->flashes, 0x0,
  782. sizeof(struct aspeed_spi_flash) * ASPEED_SPI_MAX_CS);
  783. /* Initial user mode. */
  784. for (cs = 0; cs < priv->num_cs; cs++) {
  785. priv->flashes[cs].ce_ctrl_user &= priv->info->clk_ctrl_mask;
  786. priv->flashes[cs].ce_ctrl_user |=
  787. (CTRL_STOP_ACTIVE | CTRL_IO_MODE_USER);
  788. }
  789. /*
  790. * SPI1 on AST2400 only supports CS0.
  791. * It is unnecessary to configure segment address register.
  792. */
  793. if (priv->info == &ast2400_spi_info) {
  794. priv->flashes[cs].ahb_base = plat->ahb_base;
  795. priv->flashes[cs].ahb_decoded_sz = 0x10000000;
  796. return 0;
  797. }
  798. ret = aspeed_spi_read_fixed_decoded_ranges(bus);
  799. if (ret != 0)
  800. return ret;
  801. if (!priv->fixed_decoded_range) {
  802. /* Assign basic AHB decoded size for each CS. */
  803. for (cs = 0; cs < plat->max_cs; cs++) {
  804. reg_val = readl(&priv->regs->segment_addr[cs]);
  805. decoded_sz = priv->info->segment_end(bus, reg_val) -
  806. priv->info->segment_start(bus, reg_val);
  807. if (decoded_sz < priv->info->min_decoded_sz)
  808. decoded_sz = priv->info->min_decoded_sz;
  809. priv->flashes[cs].ahb_decoded_sz = decoded_sz;
  810. }
  811. }
  812. ret = aspeed_spi_decoded_range_config(bus);
  813. return ret;
  814. }
  815. static const struct aspeed_spi_info ast2400_fmc_info = {
  816. .io_mode_mask = 0x70000000,
  817. .max_bus_width = 2,
  818. .min_decoded_sz = 0x800000,
  819. .clk_ctrl_mask = 0x00002f00,
  820. .set_4byte = ast2400_fmc_chip_set_4byte,
  821. .segment_start = ast2400_spi_segment_start,
  822. .segment_end = ast2400_spi_segment_end,
  823. .segment_reg = ast2400_spi_segment_reg,
  824. .get_clk_setting = ast2400_get_clk_setting,
  825. };
  826. static const struct aspeed_spi_info ast2400_spi_info = {
  827. .io_mode_mask = 0x70000000,
  828. .max_bus_width = 2,
  829. .min_decoded_sz = 0x800000,
  830. .clk_ctrl_mask = 0x00000f00,
  831. .set_4byte = ast2400_spi_chip_set_4byte,
  832. .segment_start = ast2400_spi_segment_start,
  833. .segment_end = ast2400_spi_segment_end,
  834. .segment_reg = ast2400_spi_segment_reg,
  835. .get_clk_setting = ast2400_get_clk_setting,
  836. };
  837. static const struct aspeed_spi_info ast2500_fmc_info = {
  838. .io_mode_mask = 0x70000000,
  839. .max_bus_width = 2,
  840. .min_decoded_sz = 0x800000,
  841. .clk_ctrl_mask = 0x00002f00,
  842. .set_4byte = ast2500_spi_chip_set_4byte,
  843. .segment_start = ast2500_spi_segment_start,
  844. .segment_end = ast2500_spi_segment_end,
  845. .segment_reg = ast2500_spi_segment_reg,
  846. .adjust_decoded_sz = ast2500_adjust_decoded_size,
  847. .get_clk_setting = ast2500_get_clk_setting,
  848. };
  849. /*
  850. * There are some different between FMC and SPI controllers.
  851. * For example, DMA operation, but this isn't implemented currently.
  852. */
  853. static const struct aspeed_spi_info ast2500_spi_info = {
  854. .io_mode_mask = 0x70000000,
  855. .max_bus_width = 2,
  856. .min_decoded_sz = 0x800000,
  857. .clk_ctrl_mask = 0x00002f00,
  858. .set_4byte = ast2500_spi_chip_set_4byte,
  859. .segment_start = ast2500_spi_segment_start,
  860. .segment_end = ast2500_spi_segment_end,
  861. .segment_reg = ast2500_spi_segment_reg,
  862. .adjust_decoded_sz = ast2500_adjust_decoded_size,
  863. .get_clk_setting = ast2500_get_clk_setting,
  864. };
  865. static const struct aspeed_spi_info ast2600_fmc_info = {
  866. .io_mode_mask = 0xf0000000,
  867. .max_bus_width = 4,
  868. .min_decoded_sz = 0x200000,
  869. .clk_ctrl_mask = 0x0f000f00,
  870. .set_4byte = ast2600_spi_chip_set_4byte,
  871. .segment_start = ast2600_spi_segment_start,
  872. .segment_end = ast2600_spi_segment_end,
  873. .segment_reg = ast2600_spi_segment_reg,
  874. .adjust_decoded_sz = ast2600_adjust_decoded_size,
  875. .get_clk_setting = ast2600_get_clk_setting,
  876. };
  877. static const struct aspeed_spi_info ast2600_spi_info = {
  878. .io_mode_mask = 0xf0000000,
  879. .max_bus_width = 4,
  880. .min_decoded_sz = 0x200000,
  881. .clk_ctrl_mask = 0x0f000f00,
  882. .set_4byte = ast2600_spi_chip_set_4byte,
  883. .segment_start = ast2600_spi_segment_start,
  884. .segment_end = ast2600_spi_segment_end,
  885. .segment_reg = ast2600_spi_segment_reg,
  886. .adjust_decoded_sz = ast2600_adjust_decoded_size,
  887. .get_clk_setting = ast2600_get_clk_setting,
  888. };
  889. static int aspeed_spi_claim_bus(struct udevice *dev)
  890. {
  891. struct udevice *bus = dev->parent;
  892. struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
  893. struct aspeed_spi_priv *priv = dev_get_priv(dev->parent);
  894. struct aspeed_spi_flash *flash = &priv->flashes[slave_plat->cs];
  895. u32 clk_setting;
  896. dev_dbg(bus, "%s: claim bus CS%u\n", bus->name, slave_plat->cs);
  897. if (flash->max_freq == 0) {
  898. clk_setting = priv->info->get_clk_setting(dev, slave_plat->max_hz);
  899. flash->ce_ctrl_user &= ~(priv->info->clk_ctrl_mask);
  900. flash->ce_ctrl_user |= clk_setting;
  901. flash->ce_ctrl_read &= ~(priv->info->clk_ctrl_mask);
  902. flash->ce_ctrl_read |= clk_setting;
  903. }
  904. return 0;
  905. }
  906. static int aspeed_spi_release_bus(struct udevice *dev)
  907. {
  908. struct udevice *bus = dev->parent;
  909. struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
  910. dev_dbg(bus, "%s: release bus CS%u\n", bus->name, slave_plat->cs);
  911. if (!aspeed_spi_get_flash(dev))
  912. return -ENODEV;
  913. return 0;
  914. }
  915. static int aspeed_spi_set_mode(struct udevice *bus, uint mode)
  916. {
  917. dev_dbg(bus, "%s: setting mode to %x\n", bus->name, mode);
  918. return 0;
  919. }
  920. static int aspeed_spi_set_speed(struct udevice *bus, uint hz)
  921. {
  922. dev_dbg(bus, "%s: setting speed to %u\n", bus->name, hz);
  923. /*
  924. * ASPEED SPI controller supports multiple CS with different
  925. * clock frequency. We cannot distinguish which CS here.
  926. * Thus, the related implementation is postponed to claim_bus.
  927. */
  928. return 0;
  929. }
  930. static int apseed_spi_of_to_plat(struct udevice *bus)
  931. {
  932. struct aspeed_spi_plat *plat = dev_get_plat(bus);
  933. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  934. int ret;
  935. struct clk hclk;
  936. priv->regs = devfdt_get_addr_index_ptr(bus, 0);
  937. if (!priv->regs) {
  938. dev_err(bus, "wrong ctrl base\n");
  939. return -EINVAL;
  940. }
  941. plat->ahb_base = devfdt_get_addr_size_index_ptr(bus, 1, &plat->ahb_sz);
  942. if (!plat->ahb_base) {
  943. dev_err(bus, "wrong AHB base\n");
  944. return -EINVAL;
  945. }
  946. plat->max_cs = dev_read_u32_default(bus, "num-cs", ASPEED_SPI_MAX_CS);
  947. if (plat->max_cs > ASPEED_SPI_MAX_CS)
  948. return -EINVAL;
  949. ret = clk_get_by_index(bus, 0, &hclk);
  950. if (ret < 0) {
  951. dev_err(bus, "%s could not get clock: %d\n", bus->name, ret);
  952. return ret;
  953. }
  954. plat->hclk_rate = clk_get_rate(&hclk);
  955. clk_free(&hclk);
  956. dev_dbg(bus, "ctrl_base = 0x%x, ahb_base = 0x%p, size = 0x%llx\n",
  957. (u32)priv->regs, plat->ahb_base, (fdt64_t)plat->ahb_sz);
  958. dev_dbg(bus, "hclk = %dMHz, max_cs = %d\n",
  959. plat->hclk_rate / 1000000, plat->max_cs);
  960. return 0;
  961. }
  962. static int aspeed_spi_probe(struct udevice *bus)
  963. {
  964. int ret;
  965. struct aspeed_spi_priv *priv = dev_get_priv(bus);
  966. struct udevice *dev;
  967. priv->info = (struct aspeed_spi_info *)dev_get_driver_data(bus);
  968. priv->num_cs = 0;
  969. for (device_find_first_child(bus, &dev); dev;
  970. device_find_next_child(&dev)) {
  971. priv->num_cs++;
  972. }
  973. if (priv->num_cs > ASPEED_SPI_MAX_CS)
  974. return -EINVAL;
  975. ret = aspeed_spi_ctrl_init(bus);
  976. return ret;
  977. }
  978. static const struct spi_controller_mem_ops aspeed_spi_mem_ops = {
  979. .supports_op = aspeed_spi_supports_op,
  980. .exec_op = aspeed_spi_exec_op_user_mode,
  981. .dirmap_create = aspeed_spi_dirmap_create,
  982. .dirmap_read = aspeed_spi_dirmap_read,
  983. };
  984. static const struct dm_spi_ops aspeed_spi_ops = {
  985. .claim_bus = aspeed_spi_claim_bus,
  986. .release_bus = aspeed_spi_release_bus,
  987. .set_speed = aspeed_spi_set_speed,
  988. .set_mode = aspeed_spi_set_mode,
  989. .mem_ops = &aspeed_spi_mem_ops,
  990. };
  991. static const struct udevice_id aspeed_spi_ids[] = {
  992. { .compatible = "aspeed,ast2400-fmc", .data = (ulong)&ast2400_fmc_info, },
  993. { .compatible = "aspeed,ast2400-spi", .data = (ulong)&ast2400_spi_info, },
  994. { .compatible = "aspeed,ast2500-fmc", .data = (ulong)&ast2500_fmc_info, },
  995. { .compatible = "aspeed,ast2500-spi", .data = (ulong)&ast2500_spi_info, },
  996. { .compatible = "aspeed,ast2600-fmc", .data = (ulong)&ast2600_fmc_info, },
  997. { .compatible = "aspeed,ast2600-spi", .data = (ulong)&ast2600_spi_info, },
  998. { }
  999. };
  1000. U_BOOT_DRIVER(aspeed_spi) = {
  1001. .name = "aspeed_spi_smc",
  1002. .id = UCLASS_SPI,
  1003. .of_match = aspeed_spi_ids,
  1004. .ops = &aspeed_spi_ops,
  1005. .of_to_plat = apseed_spi_of_to_plat,
  1006. .plat_auto = sizeof(struct aspeed_spi_plat),
  1007. .priv_auto = sizeof(struct aspeed_spi_priv),
  1008. .probe = aspeed_spi_probe,
  1009. };