1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * ASPEED AST2500 FMC/SPI Controller driver 4 * 5 * Copyright (c) 2015-2018, IBM Corporation. 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 10 #include <common.h> 11 #include <clk.h> 12 #include <dm.h> 13 #include <spi.h> 14 #include <spi_flash.h> 15 #include <asm/io.h> 16 #include <linux/ioport.h> 17 #include <malloc.h> 18 19 #define ASPEED_SPI_MAX_CS 3 20 #define FLASH_CALIBRATION_LEN 0x400 21 22 struct aspeed_spi_regs { 23 u32 conf; /* 0x00 CE Type Setting */ 24 u32 ctrl; /* 0x04 Control */ 25 u32 intr_ctrl; /* 0x08 Interrupt Control and Status */ 26 u32 cmd_ctrl; /* 0x0c Command Control */ 27 u32 ce_ctrl[ASPEED_SPI_MAX_CS]; /* 0x10 .. 0x18 CEx Control */ 28 u32 _reserved0[5]; /* .. */ 29 u32 segment_addr[ASPEED_SPI_MAX_CS]; 30 /* 0x30 .. 0x38 Segment Address */ 31 u32 _reserved1[5]; /* .. */ 32 u32 soft_rst_cmd_ctrl; /* 0x50 Auto Soft-Reset Command Control */ 33 u32 _reserved2[11]; /* .. */ 34 u32 dma_ctrl; /* 0x80 DMA Control/Status */ 35 u32 dma_flash_addr; /* 0x84 DMA Flash Side Address */ 36 u32 dma_dram_addr; /* 0x88 DMA DRAM Side Address */ 37 u32 dma_len; /* 0x8c DMA Length Register */ 38 u32 dma_checksum; /* 0x90 Checksum Calculation Result */ 39 u32 timings; /* 0x94 Read Timing Compensation */ 40 41 /* not used */ 42 u32 soft_strap_status; /* 0x9c Software Strap Status */ 43 u32 write_cmd_filter_ctrl; /* 0xa0 Write Command Filter Control */ 44 u32 write_addr_filter_ctrl; /* 0xa4 Write Address Filter Control */ 45 u32 lock_ctrl_reset; /* 0xa8 Lock Control (SRST#) */ 46 u32 lock_ctrl_wdt; /* 0xac Lock Control (Watchdog) */ 47 u32 write_addr_filter[5]; /* 0xb0 Write Address Filter */ 48 }; 49 50 /* CE Type Setting Register */ 51 #define CONF_ENABLE_W2 BIT(18) 52 #define CONF_ENABLE_W1 BIT(17) 53 #define CONF_ENABLE_W0 BIT(16) 54 #define CONF_FLASH_TYPE2 4 55 #define CONF_FLASH_TYPE1 2 /* Hardwired to SPI */ 56 #define CONF_FLASH_TYPE0 0 /* Hardwired to SPI */ 57 #define CONF_FLASH_TYPE_NOR 0x0 58 #define CONF_FLASH_TYPE_SPI 0x2 59 60 /* CE Control Register */ 61 #define CTRL_EXTENDED2 BIT(2) /* 32 bit addressing for SPI */ 62 #define CTRL_EXTENDED1 BIT(1) /* 32 bit addressing for SPI */ 63 #define CTRL_EXTENDED0 BIT(0) /* 32 bit addressing for SPI */ 64 65 /* Interrupt Control and Status Register */ 66 #define INTR_CTRL_DMA_STATUS BIT(11) 67 #define INTR_CTRL_CMD_ABORT_STATUS BIT(10) 68 #define INTR_CTRL_WRITE_PROTECT_STATUS BIT(9) 69 #define INTR_CTRL_DMA_EN BIT(3) 70 #define INTR_CTRL_CMD_ABORT_EN BIT(2) 71 #define INTR_CTRL_WRITE_PROTECT_EN BIT(1) 72 73 /* CEx Control Register */ 74 #define CE_CTRL_IO_MODE_MASK GENMASK(31, 28) 75 #define CE_CTRL_IO_QPI_DATA BIT(31) 76 #define CE_CTRL_IO_DUAL_DATA BIT(29) 77 #define CE_CTRL_IO_SINGLE 0 78 #define CE_CTRL_IO_DUAL_ADDR_DATA (BIT(29) | BIT(28)) 79 #define CE_CTRL_IO_QUAD_DATA BIT(30) 80 #define CE_CTRL_IO_QUAD_ADDR_DATA (BIT(30) | BIT(28)) 81 #define CE_CTRL_CMD_SHIFT 16 82 #define CE_CTRL_CMD_MASK 0xff 83 #define CE_CTRL_CMD(cmd) \ 84 (((cmd) & CE_CTRL_CMD_MASK) << CE_CTRL_CMD_SHIFT) 85 #define CE_CTRL_DUMMY_HIGH_SHIFT 14 86 #define CE_CTRL_DUMMY_HIGH_MASK 0x1 87 #define CE_CTRL_CLOCK_FREQ_SHIFT 8 88 #define CE_CTRL_CLOCK_FREQ_MASK 0xf 89 #define CE_CTRL_CLOCK_FREQ(div) \ 90 (((div) & CE_CTRL_CLOCK_FREQ_MASK) << CE_CTRL_CLOCK_FREQ_SHIFT) 91 #define CE_G6_CTRL_CLOCK_FREQ(div) \ 92 ((((div) & CE_CTRL_CLOCK_FREQ_MASK) << CE_CTRL_CLOCK_FREQ_SHIFT) | (((div) & 0xf0) << 24)) 93 #define CE_CTRL_DUMMY_LOW_SHIFT 6 /* 2 bits [7:6] */ 94 #define CE_CTRL_DUMMY_LOW_MASK 0x3 95 #define CE_CTRL_DUMMY(dummy) \ 96 (((((dummy) >> 2) & CE_CTRL_DUMMY_HIGH_MASK) \ 97 << CE_CTRL_DUMMY_HIGH_SHIFT) | \ 98 (((dummy) & CE_CTRL_DUMMY_LOW_MASK) << CE_CTRL_DUMMY_LOW_SHIFT)) 99 #define CE_CTRL_STOP_ACTIVE BIT(2) 100 #define CE_CTRL_MODE_MASK 0x3 101 #define CE_CTRL_READMODE 0x0 102 #define CE_CTRL_FREADMODE 0x1 103 #define CE_CTRL_WRITEMODE 0x2 104 #define CE_CTRL_USERMODE 0x3 105 #define CE_CTRL_FREQ_MASK 0xf0fff0ff 106 107 #define SPI_READ_FROM_FLASH 0x00000001 108 #define SPI_WRITE_TO_FLASH 0x00000002 109 110 /* Auto Soft-Reset Command Control */ 111 #define SOFT_RST_CMD_EN GENMASK(1, 0) 112 113 /* 114 * The Segment Register uses a 8MB unit to encode the start address 115 * and the end address of the AHB window of a SPI flash device. 116 * Default segment addresses are : 117 * 118 * CE0 0x20000000 - 0x2fffffff 128MB 119 * CE1 0x28000000 - 0x29ffffff 32MB 120 * CE2 0x2a000000 - 0x2bffffff 32MB 121 * 122 * The full address space of the AHB window of the controller is 123 * covered and CE0 start address and CE2 end addresses are read-only. 124 */ 125 #define SEGMENT_ADDR_START(reg) ((((reg) >> 16) & 0xff) << 23) 126 #define SEGMENT_ADDR_END(reg) ((((reg) >> 24) & 0xff) << 23) 127 #define SEGMENT_ADDR_VALUE(start, end) \ 128 (((((start) >> 23) & 0xff) << 16) | ((((end) >> 23) & 0xff) << 24)) 129 130 #define G6_SEGMENT_ADDR_START(reg) (((reg) << 16) & 0x0ff00000) 131 #define G6_SEGMENT_ADDR_END(reg) (((reg) & 0x0ff00000) + 0x100000) 132 #define G6_SEGMENT_ADDR_VALUE(start, end) \ 133 ((((start) & 0x0ff00000) >> 16) | (((end) - 0x100000) & 0xffff0000)) 134 135 /* DMA Control/Status Register */ 136 #define DMA_CTRL_DELAY_SHIFT 8 137 #define DMA_CTRL_DELAY_MASK 0xf 138 #define G6_DMA_CTRL_DELAY_MASK 0xff 139 #define DMA_CTRL_FREQ_SHIFT 4 140 #define G6_DMA_CTRL_FREQ_SHIFT 16 141 142 #define DMA_CTRL_FREQ_MASK 0xf 143 #define TIMING_MASK(div, delay) \ 144 (((delay & DMA_CTRL_DELAY_MASK) << DMA_CTRL_DELAY_SHIFT) | \ 145 ((div & DMA_CTRL_FREQ_MASK) << DMA_CTRL_FREQ_SHIFT)) 146 #define G6_TIMING_MASK(div, delay) \ 147 (((delay & G6_DMA_CTRL_DELAY_MASK) << DMA_CTRL_DELAY_SHIFT) | \ 148 ((div & DMA_CTRL_FREQ_MASK) << G6_DMA_CTRL_FREQ_SHIFT)) 149 #define DAM_CTRL_REQUEST BIT(31) 150 #define DAM_CTRL_GRANT BIT(30) 151 #define DMA_CTRL_CALIB BIT(3) 152 #define DMA_CTRL_CKSUM BIT(2) 153 #define DMA_CTRL_WRITE BIT(1) 154 #define DMA_CTRL_ENABLE BIT(0) 155 156 #define DMA_GET_REQ_MAGIC 0xaeed0000 157 #define DMA_DISCARD_REQ_MAGIC 0xdeea0000 158 159 /* for ast2600 setting */ 160 #define SPI_3B_AUTO_CLR_REG 0x1e6e2510 161 #define SPI_3B_AUTO_CLR BIT(9) 162 163 164 /* 165 * flash related info 166 */ 167 struct aspeed_spi_flash { 168 u8 cs; 169 /* Initialized when the SPI bus is 170 * first claimed 171 */ 172 bool init; 173 void __iomem *ahb_base; /* AHB Window for this device */ 174 u32 ahb_size; /* AHB Window segment size */ 175 u32 ce_ctrl_user; /* CE Control Register for USER mode */ 176 u32 ce_ctrl_fread; /* CE Control Register for FREAD mode */ 177 u32 read_iomode; 178 u32 write_iomode; 179 u32 max_freq; 180 struct spi_flash *spi; /* Associated SPI Flash device */ 181 }; 182 183 struct aspeed_spi_priv { 184 struct aspeed_spi_regs *regs; 185 void __iomem *ahb_base; /* AHB Window for all flash devices */ 186 int new_ver; 187 u32 ahb_size; /* AHB Window segments size */ 188 ulong hclk_rate; /* AHB clock rate */ 189 u8 num_cs; 190 bool is_fmc; 191 192 struct aspeed_spi_flash flashes[ASPEED_SPI_MAX_CS]; 193 u32 flash_count; 194 195 u8 cmd_buf[16]; /* SPI command in progress */ 196 size_t cmd_len; 197 }; 198 199 static struct aspeed_spi_flash *aspeed_spi_get_flash(struct udevice *dev) 200 { 201 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev); 202 struct aspeed_spi_priv *priv = dev_get_priv(dev->parent); 203 u8 cs = slave_plat->cs; 204 205 if (cs >= priv->flash_count) { 206 pr_err("invalid CS %u\n", cs); 207 return NULL; 208 } 209 210 return &priv->flashes[cs]; 211 } 212 213 static u32 aspeed_g6_spi_hclk_divisor(struct aspeed_spi_priv *priv, u32 max_hz) 214 { 215 u32 hclk_rate = priv->hclk_rate; 216 /* HCLK/1 .. HCLK/16 */ 217 const u8 hclk_masks[] = { 218 15, 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8, 0 219 }; 220 u8 hclk_div = 0x4; /* default value */ 221 bool found = false; 222 u32 i, j = 0; 223 224 /* FMC/SPIR10[27:24] */ 225 for (j = 0; j < 0xf; i++) { 226 for (i = 0; i < ARRAY_SIZE(hclk_masks); i++) { 227 if (i == 0 && j == 0) 228 continue; 229 230 if ((hclk_rate / ((i + 1) + j * 16)) <= max_hz) { 231 found = 1; 232 break; 233 } 234 } 235 236 if (found) 237 break; 238 } 239 240 debug("hclk=%d required=%d h_div %d, divisor is %d (mask %x) speed=%d\n", 241 hclk_rate, max_hz, j, i + 1, hclk_masks[i], hclk_rate / (i + 1 + j * 16)); 242 243 hclk_div = ((j << 4) | hclk_masks[i]); 244 245 return hclk_div; 246 } 247 248 static u32 aspeed_spi_hclk_divisor(struct aspeed_spi_priv *priv, u32 max_hz) 249 { 250 u32 hclk_rate = priv->hclk_rate; 251 /* HCLK/1 .. HCLK/16 */ 252 const u8 hclk_masks[] = { 253 15, 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8, 0 254 }; 255 u32 i; 256 u32 hclk_div_setting = 0; 257 258 for (i = 0; i < ARRAY_SIZE(hclk_masks); i++) { 259 if (max_hz >= (hclk_rate / (i + 1))) 260 break; 261 } 262 debug("hclk=%d required=%d divisor is %d (mask %x) speed=%d\n", 263 hclk_rate, max_hz, i + 1, hclk_masks[i], hclk_rate / (i + 1)); 264 265 hclk_div_setting = hclk_masks[i]; 266 267 return hclk_div_setting; 268 } 269 270 /* 271 * Use some address/size under the first flash device CE0 272 */ 273 static u32 aspeed_spi_fmc_checksum(struct aspeed_spi_priv *priv, 274 struct aspeed_spi_flash *flash, 275 u8 div, u8 delay) 276 { 277 u32 flash_addr = (u32)flash->ahb_base + 0x10000; 278 u32 dma_ctrl; 279 u32 checksum; 280 281 writel(flash_addr, &priv->regs->dma_flash_addr); 282 writel(FLASH_CALIBRATION_LEN, &priv->regs->dma_len); 283 284 /* 285 * When doing calibration, the SPI clock rate in the CE0 286 * Control Register and the data input delay cycles in the 287 * Read Timing Compensation Register are replaced by bit[11:4]. 288 */ 289 dma_ctrl = DMA_CTRL_ENABLE | DMA_CTRL_CKSUM | DMA_CTRL_CALIB | 290 TIMING_MASK(div, delay); 291 292 writel(dma_ctrl, &priv->regs->dma_ctrl); 293 while (!(readl(&priv->regs->intr_ctrl) & INTR_CTRL_DMA_STATUS)) 294 ; 295 296 writel(0x0, &priv->regs->intr_ctrl); 297 298 checksum = readl(&priv->regs->dma_checksum); 299 300 writel(0x0, &priv->regs->dma_ctrl); 301 return checksum; 302 } 303 304 /* 305 * Use some address/size under the first flash device CE0 306 */ 307 static u32 aspeed_g6_spi_fmc_checksum(struct aspeed_spi_priv *priv, 308 struct aspeed_spi_flash *flash, 309 u8 div, u8 delay) 310 { 311 u32 flash_addr = (u32)flash->ahb_base; 312 u32 dma_ctrl; 313 u32 checksum; 314 315 writel(DMA_GET_REQ_MAGIC, &priv->regs->dma_ctrl); 316 if (readl(&priv->regs->dma_ctrl) & DAM_CTRL_REQUEST) { 317 while (!(readl(&priv->regs->dma_ctrl) & DAM_CTRL_GRANT)) 318 ; 319 } 320 321 writel(flash_addr, &priv->regs->dma_flash_addr); 322 writel(FLASH_CALIBRATION_LEN, &priv->regs->dma_len); 323 324 /* 325 * When doing calibration, the SPI clock rate in the control 326 * register and the data input delay cycles in the 327 * read timing compensation register are replaced by bit[11:4]. 328 */ 329 dma_ctrl = DMA_CTRL_ENABLE | DMA_CTRL_CKSUM | DMA_CTRL_CALIB | 330 G6_TIMING_MASK(div, delay); 331 332 writel(dma_ctrl, &priv->regs->dma_ctrl); 333 while (!(readl(&priv->regs->intr_ctrl) & INTR_CTRL_DMA_STATUS)) 334 ; 335 336 checksum = readl(&priv->regs->dma_checksum); 337 338 writel(0x0, &priv->regs->intr_ctrl); 339 writel(0x0, &priv->regs->dma_ctrl); 340 writel(DMA_DISCARD_REQ_MAGIC, &priv->regs->dma_ctrl); 341 342 return checksum; 343 } 344 345 static u32 aspeed_spi_read_checksum(struct aspeed_spi_priv *priv, 346 struct aspeed_spi_flash *flash, 347 u8 div, u8 delay) 348 { 349 if (priv->new_ver) 350 return aspeed_g6_spi_fmc_checksum(priv, flash, div, delay); 351 352 /* for AST2500, */ 353 if (!priv->is_fmc) { 354 pr_warn("No timing calibration support for SPI controllers"); 355 return 0xbadc0de; 356 } 357 358 return aspeed_spi_fmc_checksum(priv, flash, div, delay); 359 } 360 361 #define TIMING_DELAY_DI_4NS BIT(3) 362 #define TIMING_DELAY_HCYCLE_MAX 5 363 364 /* 365 * Check whether the data is not all 0 or 1 in order to 366 * avoid calibriate umount spi-flash. 367 */ 368 static bool aspeed_spi_calibriation_enable(const u8 *buf, u32 sz) 369 { 370 const u32 *buf_32 = (const u32 *)buf; 371 u32 i; 372 u32 valid_count = 0; 373 374 for (i = 0; i < (sz / 4); i++) { 375 if (buf_32[i] != 0 && buf_32[i] != 0xffffffff) 376 valid_count++; 377 if (valid_count > 100) 378 return true; 379 } 380 381 return false; 382 } 383 384 static int get_mid_point_of_longest_one(u8 *buf, u32 len) 385 { 386 int i; 387 int start = 0, mid_point = 0; 388 int max_cnt = 0, cnt = 0; 389 390 for (i = 0; i < len; i++) { 391 if (buf[i] == 1) { 392 cnt++; 393 } else { 394 cnt = 0; 395 start = i; 396 } 397 398 if (max_cnt < cnt) { 399 max_cnt = cnt; 400 mid_point = start + (cnt / 2); 401 } 402 } 403 404 /* 405 * In order to get a stable SPI read timing, 406 * abandon the result if the length of longest 407 * consecutive good points is too short. 408 */ 409 if (max_cnt < 4) 410 return -1; 411 412 return mid_point; 413 } 414 415 static int aspeed_spi_timing_calibration(struct aspeed_spi_priv *priv, 416 struct aspeed_spi_flash *flash) 417 { 418 u32 cs = flash->cs; 419 /* HCLK/5 .. HCLK/1 */ 420 const u8 hclk_masks[] = {13, 6, 14, 7, 15}; 421 u32 timing_reg; 422 u32 checksum, gold_checksum; 423 int i; 424 u32 hcycle, delay_ns; 425 u32 final_delay = 0; 426 u32 hclk_div = 0; 427 u32 max_freq = flash->max_freq; 428 u32 reg_val; 429 u8 *tmp_buf = NULL; 430 u8 *calib_res = NULL; 431 int calib_point; 432 bool pass; 433 434 if (priv->new_ver) { 435 timing_reg = readl(&priv->regs->timings + cs); 436 if (timing_reg != 0) 437 return 0; 438 439 /* 440 * use the related low frequency to get check calibration data 441 * and get golden data. 442 */ 443 reg_val = flash->ce_ctrl_fread & CE_CTRL_FREQ_MASK; 444 writel(reg_val, &priv->regs->ce_ctrl[cs]); 445 tmp_buf = (u8 *)malloc(FLASH_CALIBRATION_LEN); 446 if (!tmp_buf) 447 return -ENOMEM; 448 449 memcpy_fromio(tmp_buf, flash->ahb_base, FLASH_CALIBRATION_LEN); 450 if (!aspeed_spi_calibriation_enable(tmp_buf, FLASH_CALIBRATION_LEN)) { 451 debug("flash data is monotonous, skip calibration.\n"); 452 goto no_calib; 453 } 454 455 /* Compute reference checksum at lowest freq HCLK/16 */ 456 gold_checksum = aspeed_spi_read_checksum(priv, flash, 0, 0); 457 458 /* 459 * allocate a space to record calibration result for 460 * different timing compensation with fixed 461 * HCLK division. 462 */ 463 calib_res = (u8 *)malloc(6 * 17); 464 if (!calib_res) { 465 free(tmp_buf); 466 return -ENOMEM; 467 } 468 469 /* from HCLK/2 to HCLK/5 */ 470 for (i = 0; i < ARRAY_SIZE(hclk_masks) - 1; i++) { 471 if (priv->hclk_rate / (i + 2) > max_freq) { 472 debug("skipping freq %ld\n", priv->hclk_rate / (i + 2)); 473 continue; 474 } 475 476 max_freq = (u32)priv->hclk_rate / (i + 2); 477 478 memset(calib_res, 0x0, 6 * 17); 479 for (hcycle = 0; hcycle <= 5; hcycle++) { 480 /* increase DI delay by the step of 0.5ns */ 481 debug("Delay Enable : hcycle %x\n", hcycle); 482 for (delay_ns = 0; delay_ns <= 0xf; delay_ns++) { 483 checksum = aspeed_g6_spi_fmc_checksum(priv, flash, 484 hclk_masks[3 - i], 485 TIMING_DELAY_DI_4NS | hcycle | (delay_ns << 4)); 486 pass = (checksum == gold_checksum); 487 calib_res[hcycle * 17 + delay_ns] = pass; 488 debug("HCLK/%d, %d HCLK cycle, %d delay_ns : %s\n", 489 i + 2, hcycle, delay_ns, pass ? "PASS" : "FAIL"); 490 } 491 } 492 493 calib_point = get_mid_point_of_longest_one(calib_res, 6 * 17); 494 if (calib_point < 0) { 495 debug("cannot get good calibration point.\n"); 496 continue; 497 } 498 499 hcycle = calib_point / 17; 500 delay_ns = calib_point % 17; 501 debug("final hcycle: %d, delay_ns: %d\n", hcycle, delay_ns); 502 503 final_delay = (TIMING_DELAY_DI_4NS | hcycle | (delay_ns << 4)) << (i * 8); 504 writel(final_delay, &priv->regs->timings + cs); 505 break; 506 } 507 508 no_calib: 509 hclk_div = aspeed_g6_spi_hclk_divisor(priv, max_freq); 510 /* configure SPI clock frequency */ 511 reg_val = readl(&priv->regs->ce_ctrl[cs]); 512 reg_val = (reg_val & CE_CTRL_FREQ_MASK) | CE_G6_CTRL_CLOCK_FREQ(hclk_div); 513 writel(reg_val, &priv->regs->ce_ctrl[cs]); 514 515 /* add clock setting info for CE ctrl setting */ 516 flash->ce_ctrl_user = 517 (flash->ce_ctrl_user & CE_CTRL_FREQ_MASK) | CE_G6_CTRL_CLOCK_FREQ(hclk_div); 518 flash->ce_ctrl_fread = 519 (flash->ce_ctrl_fread & CE_CTRL_FREQ_MASK) | CE_G6_CTRL_CLOCK_FREQ(hclk_div); 520 521 debug("cs: %d, freq: %dMHz\n", cs, max_freq / 1000000); 522 523 if (tmp_buf) 524 free(tmp_buf); 525 if (calib_res) 526 free(calib_res); 527 } else { 528 /* Use the ctrl setting in aspeed_spi_flash_init() to 529 * implement calibration process. 530 */ 531 timing_reg = readl(&priv->regs->timings); 532 if (timing_reg != 0) 533 return 0; 534 535 /* Compute reference checksum at lowest freq HCLK/16 */ 536 gold_checksum = aspeed_spi_read_checksum(priv, flash, 0, 0); 537 538 for (i = 0; i < ARRAY_SIZE(hclk_masks); i++) { 539 u32 hdiv = 5 - i; 540 u32 hshift = (hdiv - 1) << 2; 541 bool pass = false; 542 u8 delay; 543 544 if (priv->hclk_rate / hdiv > flash->max_freq) { 545 debug("skipping freq %ld\n", priv->hclk_rate / hdiv); 546 continue; 547 } 548 549 /* Increase HCLK cycles until read succeeds */ 550 for (hcycle = 0; hcycle <= TIMING_DELAY_HCYCLE_MAX; hcycle++) { 551 /* Try first with a 4ns DI delay */ 552 delay = TIMING_DELAY_DI_4NS | hcycle; 553 checksum = aspeed_spi_read_checksum(priv, flash, hclk_masks[i], 554 delay); 555 pass = (checksum == gold_checksum); 556 debug(" HCLK/%d, 4ns DI delay, %d HCLK cycle : %s\n", 557 hdiv, hcycle, pass ? "PASS" : "FAIL"); 558 559 /* Try again with more HCLK cycles */ 560 if (!pass) 561 continue; 562 563 /* Try without the 4ns DI delay */ 564 delay = hcycle; 565 checksum = aspeed_spi_read_checksum(priv, flash, hclk_masks[i], 566 delay); 567 pass = (checksum == gold_checksum); 568 debug(" HCLK/%d, no DI delay, %d HCLK cycle : %s\n", 569 hdiv, hcycle, pass ? "PASS" : "FAIL"); 570 571 /* All good for this freq */ 572 if (pass) 573 break; 574 } 575 576 if (pass) { 577 timing_reg &= ~(0xfu << hshift); 578 timing_reg |= delay << hshift; 579 } 580 } 581 582 debug("Read Timing Compensation set to 0x%08x\n", timing_reg); 583 writel(timing_reg, &priv->regs->timings); 584 } 585 586 return 0; 587 } 588 589 static int aspeed_spi_controller_init(struct aspeed_spi_priv *priv) 590 { 591 int cs; 592 593 /* 594 * Enable write on all flash devices as USER command mode 595 * requires it. 596 */ 597 setbits_le32(&priv->regs->conf, 598 CONF_ENABLE_W2 | CONF_ENABLE_W1 | CONF_ENABLE_W0); 599 600 /* 601 * Set safe default settings for each device. These will be 602 * tuned after the SPI flash devices are probed. 603 */ 604 if (priv->new_ver) { 605 for (cs = 0; cs < priv->flash_count; cs++) { 606 struct aspeed_spi_flash *flash = &priv->flashes[cs]; 607 u32 addr_config = 0; 608 switch(cs) { 609 case 0: 610 flash->ahb_base = priv->ahb_base; 611 debug("cs0 mem-map : %x\n", (u32)flash->ahb_base); 612 break; 613 case 1: 614 flash->ahb_base = priv->flashes[0].ahb_base + 0x4000000; /* cs0 + 64MB */ 615 debug("cs1 mem-map : %x end %x\n", 616 (u32)flash->ahb_base, (u32)flash->ahb_base + 0x4000000); 617 break; 618 case 2: 619 flash->ahb_base = priv->flashes[0].ahb_base + 0x4000000 * 2; /* cs0 + 128MB : use 64MB */ 620 debug("cs2 mem-map : %x end %x\n", 621 (u32)flash->ahb_base, (u32)flash->ahb_base + 0x4000000); 622 break; 623 } 624 addr_config = 625 G6_SEGMENT_ADDR_VALUE((u32)flash->ahb_base, (u32)flash->ahb_base + 0x4000000); 626 writel(addr_config, &priv->regs->segment_addr[cs]); 627 flash->cs = cs; 628 flash->ce_ctrl_user = CE_CTRL_USERMODE; 629 flash->ce_ctrl_fread = CE_CTRL_READMODE; 630 } 631 } else { 632 for (cs = 0; cs < priv->flash_count; cs++) { 633 struct aspeed_spi_flash *flash = &priv->flashes[cs]; 634 u32 seg_addr = readl(&priv->regs->segment_addr[cs]); 635 /* 636 * The start address of the AHB window of CE0 is 637 * read-only and is the same as the address of the 638 * overall AHB window of the controller for all flash 639 * devices. 640 */ 641 flash->ahb_base = cs ? (void *)SEGMENT_ADDR_START(seg_addr) : 642 priv->ahb_base; 643 644 flash->cs = cs; 645 flash->ce_ctrl_user = CE_CTRL_USERMODE; 646 flash->ce_ctrl_fread = CE_CTRL_READMODE; 647 } 648 } 649 return 0; 650 } 651 652 static int aspeed_spi_read_from_ahb(void __iomem *ahb_base, void *buf, 653 size_t len) 654 { 655 size_t offset = 0; 656 657 if (!((uintptr_t)buf % 4)) { 658 readsl(ahb_base, buf, len >> 2); 659 offset = len & ~0x3; 660 len -= offset; 661 } 662 readsb(ahb_base, (u8 *)buf + offset, len); 663 664 return 0; 665 } 666 667 static int aspeed_spi_write_to_ahb(void __iomem *ahb_base, const void *buf, 668 size_t len) 669 { 670 size_t offset = 0; 671 672 if (!((uintptr_t)buf % 4)) { 673 writesl(ahb_base, buf, len >> 2); 674 offset = len & ~0x3; 675 len -= offset; 676 } 677 writesb(ahb_base, (u8 *)buf + offset, len); 678 679 return 0; 680 } 681 682 static void aspeed_spi_start_user(struct aspeed_spi_priv *priv, 683 struct aspeed_spi_flash *flash) 684 { 685 u32 ctrl_reg = flash->ce_ctrl_user | CE_CTRL_STOP_ACTIVE; 686 687 /* Deselect CS and set USER command mode */ 688 writel(ctrl_reg, &priv->regs->ce_ctrl[flash->cs]); 689 690 /* Select CS */ 691 clrbits_le32(&priv->regs->ce_ctrl[flash->cs], CE_CTRL_STOP_ACTIVE); 692 } 693 694 static void aspeed_spi_stop_user(struct aspeed_spi_priv *priv, 695 struct aspeed_spi_flash *flash) 696 { 697 /* Deselect CS first */ 698 setbits_le32(&priv->regs->ce_ctrl[flash->cs], CE_CTRL_STOP_ACTIVE); 699 700 /* Restore default command mode */ 701 writel(flash->ce_ctrl_fread, &priv->regs->ce_ctrl[flash->cs]); 702 } 703 704 static int aspeed_spi_read_reg(struct aspeed_spi_priv *priv, 705 struct aspeed_spi_flash *flash, 706 u8 opcode, u8 *read_buf, int len) 707 { 708 aspeed_spi_start_user(priv, flash); 709 aspeed_spi_write_to_ahb(flash->ahb_base, &opcode, 1); 710 aspeed_spi_read_from_ahb(flash->ahb_base, read_buf, len); 711 aspeed_spi_stop_user(priv, flash); 712 713 return 0; 714 } 715 716 static int aspeed_spi_write_reg(struct aspeed_spi_priv *priv, 717 struct aspeed_spi_flash *flash, 718 u8 opcode, const u8 *write_buf, int len) 719 { 720 aspeed_spi_start_user(priv, flash); 721 aspeed_spi_write_to_ahb(flash->ahb_base, &opcode, 1); 722 aspeed_spi_write_to_ahb(flash->ahb_base, write_buf, len); 723 aspeed_spi_stop_user(priv, flash); 724 725 debug("=== write opcode [%x] ==== \n", opcode); 726 switch(opcode) { 727 case SPINOR_OP_EN4B: 728 /* For ast2600, if 2 chips ABR mode is enabled, 729 * turn on 3B mode auto clear in order to avoid 730 * the scenario where spi controller is at 4B mode 731 * and flash site is at 3B mode after 3rd switch. 732 */ 733 if (priv->new_ver == 1 && (readl(SPI_3B_AUTO_CLR_REG) & SPI_3B_AUTO_CLR)) 734 writel(readl(&priv->regs->soft_rst_cmd_ctrl) | SOFT_RST_CMD_EN, 735 &priv->regs->soft_rst_cmd_ctrl); 736 737 writel(readl(&priv->regs->ctrl) | (0x11 << flash->cs), &priv->regs->ctrl); 738 break; 739 case SPINOR_OP_EX4B: 740 writel(readl(&priv->regs->ctrl) & ~(0x11 << flash->cs), &priv->regs->ctrl); 741 break; 742 } 743 return 0; 744 } 745 746 static void aspeed_spi_send_cmd_addr(struct aspeed_spi_priv *priv, 747 struct aspeed_spi_flash *flash, 748 const u8 *cmdbuf, unsigned int cmdlen, uint32_t flag) 749 { 750 int i; 751 752 /* First, send the opcode */ 753 aspeed_spi_write_to_ahb(flash->ahb_base, &cmdbuf[0], 1); 754 755 if(flash->write_iomode == CE_CTRL_IO_QUAD_ADDR_DATA && (flag & SPI_WRITE_TO_FLASH)) 756 writel(flash->ce_ctrl_user | flash->write_iomode, &priv->regs->ce_ctrl[flash->cs]); 757 else if(flash->read_iomode == CE_CTRL_IO_QUAD_ADDR_DATA && (flag & SPI_READ_FROM_FLASH)) 758 writel(flash->ce_ctrl_user | flash->read_iomode, &priv->regs->ce_ctrl[flash->cs]); 759 760 /* Then the address */ 761 for (i = 1 ; i < cmdlen; i++) 762 aspeed_spi_write_to_ahb(flash->ahb_base, &cmdbuf[i], 1); 763 } 764 765 static ssize_t aspeed_spi_read_user(struct aspeed_spi_priv *priv, 766 struct aspeed_spi_flash *flash, 767 unsigned int cmdlen, const u8 *cmdbuf, 768 unsigned int len, u8 *read_buf) 769 { 770 u8 dummy = 0x00; 771 int i; 772 773 aspeed_spi_start_user(priv, flash); 774 775 /* cmd buffer = cmd + addr + dummies */ 776 aspeed_spi_send_cmd_addr(priv, flash, cmdbuf, 777 cmdlen - (flash->spi->read_dummy / 8), SPI_READ_FROM_FLASH); 778 779 for (i = 0; i < (flash->spi->read_dummy / 8); i++) 780 aspeed_spi_write_to_ahb(flash->ahb_base, &dummy, 1); 781 782 if (flash->read_iomode) { 783 clrbits_le32(&priv->regs->ce_ctrl[flash->cs], 784 CE_CTRL_IO_MODE_MASK); 785 setbits_le32(&priv->regs->ce_ctrl[flash->cs], flash->read_iomode); 786 } 787 788 aspeed_spi_read_from_ahb(flash->ahb_base, read_buf, len); 789 aspeed_spi_stop_user(priv, flash); 790 791 return 0; 792 } 793 794 static ssize_t aspeed_spi_read_sfdp(struct aspeed_spi_priv *priv, 795 struct aspeed_spi_flash *flash, 796 unsigned int cmdlen, const u8 *cmdbuf, 797 unsigned int len, u8 *read_buf) 798 { 799 u8 dummy = 0x00; 800 int i; 801 802 /* only 1-1-1 mode is used to read SFDP */ 803 aspeed_spi_start_user(priv, flash); 804 805 /* cmd buffer = cmd + addr + dummies */ 806 aspeed_spi_send_cmd_addr(priv, flash, cmdbuf, 807 cmdlen - (flash->spi->read_dummy / 8), 0); 808 809 for (i = 0; i < (flash->spi->read_dummy / 8); i++) 810 aspeed_spi_write_to_ahb(flash->ahb_base, &dummy, 1); 811 812 aspeed_spi_read_from_ahb(flash->ahb_base, read_buf, len); 813 aspeed_spi_stop_user(priv, flash); 814 815 return 0; 816 } 817 818 static ssize_t aspeed_spi_write_user(struct aspeed_spi_priv *priv, 819 struct aspeed_spi_flash *flash, 820 unsigned int cmdlen, const u8 *cmdbuf, 821 unsigned int len, const u8 *write_buf) 822 { 823 aspeed_spi_start_user(priv, flash); 824 825 /* cmd buffer = cmd + addr : normally cmd is use signle mode*/ 826 aspeed_spi_send_cmd_addr(priv, flash, cmdbuf, cmdlen, SPI_WRITE_TO_FLASH); 827 828 /* data will use io mode */ 829 if(flash->write_iomode == CE_CTRL_IO_QUAD_DATA) 830 writel(flash->ce_ctrl_user | flash->write_iomode, &priv->regs->ce_ctrl[flash->cs]); 831 832 aspeed_spi_write_to_ahb(flash->ahb_base, write_buf, len); 833 834 aspeed_spi_stop_user(priv, flash); 835 836 return 0; 837 } 838 839 static u32 aspeed_spi_flash_to_addr(struct aspeed_spi_flash *flash, 840 const u8 *cmdbuf, unsigned int cmdlen) 841 { 842 u8 addrlen = cmdlen - 1; 843 u32 addr = (cmdbuf[1] << 16) | (cmdbuf[2] << 8) | cmdbuf[3]; 844 845 /* 846 * U-Boot SPI Flash layer uses 3 bytes addresses, but it might 847 * change one day 848 */ 849 if (addrlen == 4) 850 addr = (addr << 8) | cmdbuf[4]; 851 852 return addr; 853 } 854 855 /* TODO(clg@kaod.org): add support for XFER_MMAP instead ? */ 856 static ssize_t aspeed_spi_read(struct aspeed_spi_priv *priv, 857 struct aspeed_spi_flash *flash, 858 unsigned int cmdlen, const u8 *cmdbuf, 859 unsigned int len, u8 *read_buf) 860 { 861 /* cmd buffer = cmd + addr + dummies */ 862 u32 offset = aspeed_spi_flash_to_addr(flash, cmdbuf, 863 cmdlen - (flash->spi->read_dummy/8)); 864 865 /* 866 * Switch to USER command mode: 867 * - if read SFDP content. 868 * - if the AHB window configured for the device is 869 * too small for the read operation 870 * - if read offset is smaller than the decoded start address 871 * and the decoded range is not multiple of flash size. 872 */ 873 if ((offset + len >= flash->ahb_size) || \ 874 (offset < ((int)flash->ahb_base & 0x0FFFFFFF) && \ 875 (((int)flash->ahb_base & 0x0FFFFFFF) % flash->spi->size) != 0)) { 876 return aspeed_spi_read_user(priv, flash, cmdlen, cmdbuf, 877 len, read_buf); 878 } 879 880 memcpy_fromio(read_buf, flash->ahb_base + offset, len); 881 882 return 0; 883 } 884 885 static int aspeed_spi_xfer(struct udevice *dev, unsigned int bitlen, 886 const void *dout, void *din, unsigned long flags) 887 { 888 struct udevice *bus = dev->parent; 889 struct aspeed_spi_priv *priv = dev_get_priv(bus); 890 struct aspeed_spi_flash *flash; 891 u8 *cmd_buf = priv->cmd_buf; 892 size_t data_bytes; 893 int err = 0; 894 895 flash = aspeed_spi_get_flash(dev); 896 if (!flash) 897 return -ENXIO; 898 899 if (flags & SPI_XFER_BEGIN) { 900 /* save command in progress */ 901 priv->cmd_len = bitlen / 8; 902 memcpy(cmd_buf, dout, priv->cmd_len); 903 } 904 905 if (flags == (SPI_XFER_BEGIN | SPI_XFER_END)) { 906 /* if start and end bit are set, the data bytes is 0. */ 907 data_bytes = 0; 908 } else { 909 data_bytes = bitlen / 8; 910 } 911 912 debug("CS%u: %s cmd %zu bytes data %zu bytes\n", flash->cs, 913 din ? "read" : "write", priv->cmd_len, data_bytes); 914 915 if ((flags & SPI_XFER_END) || flags == 0) { 916 if (priv->cmd_len == 0) { 917 pr_err("No command is progress !\n"); 918 return -1; 919 } 920 921 if (din && data_bytes) { 922 if (priv->cmd_len == 1) { 923 err = aspeed_spi_read_reg(priv, flash, 924 cmd_buf[0], 925 din, data_bytes); 926 } else if (cmd_buf[0] == SPINOR_OP_RDSFDP) { 927 err = aspeed_spi_read_sfdp(priv, flash, 928 priv->cmd_len, 929 cmd_buf, data_bytes, 930 din); 931 } else { 932 err = aspeed_spi_read(priv, flash, 933 priv->cmd_len, 934 cmd_buf, data_bytes, 935 din); 936 } 937 } else if (dout) { 938 if (priv->cmd_len == 1) { 939 err = aspeed_spi_write_reg(priv, flash, 940 cmd_buf[0], 941 dout, data_bytes); 942 } else { 943 err = aspeed_spi_write_user(priv, flash, 944 priv->cmd_len, 945 cmd_buf, data_bytes, 946 dout); 947 } 948 } 949 950 if (flags & SPI_XFER_END) { 951 /* clear command */ 952 memset(cmd_buf, 0, sizeof(priv->cmd_buf)); 953 priv->cmd_len = 0; 954 } 955 } 956 957 return err; 958 } 959 960 static int aspeed_spi_child_pre_probe(struct udevice *dev) 961 { 962 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev); 963 964 debug("pre_probe slave device on CS%u, max_hz %u, mode 0x%x.\n", 965 slave_plat->cs, slave_plat->max_hz, slave_plat->mode); 966 967 if (!aspeed_spi_get_flash(dev)) 968 return -ENXIO; 969 970 return 0; 971 } 972 973 /* 974 * AST2600 SPI memory controllers support multiple chip selects. 975 * The start address of a decode range should be multiple 976 * of its related flash size. Namely, the total decoded size 977 * from flash 0 to flash N should be multiple of (N + 1) flash size. 978 */ 979 void aspeed_g6_adjust_decode_sz(u32 decode_sz_arr[], int len) 980 { 981 int cs, j; 982 u32 sz; 983 984 for (cs = len - 1; cs >= 0; cs--) { 985 sz = 0; 986 for (j = 0; j < cs; j++) 987 sz += decode_sz_arr[j]; 988 989 if (sz % decode_sz_arr[cs] != 0) 990 decode_sz_arr[0] += (sz % decode_sz_arr[cs]); 991 } 992 } 993 994 /* 995 * It is possible to automatically define a contiguous address space 996 * on top of all CEs in the AHB window of the controller but it would 997 * require much more work. Let's start with a simple mapping scheme 998 * which should work fine for a single flash device. 999 * 1000 * More complex schemes should probably be defined with the device 1001 * tree. 1002 */ 1003 static int aspeed_spi_flash_set_segment(struct aspeed_spi_priv *priv, 1004 struct aspeed_spi_flash *flash) 1005 { 1006 u32 seg_addr; 1007 u32 decode_sz_arr[ASPEED_SPI_MAX_CS]; 1008 u32 reg_val; 1009 u32 cs; 1010 u32 total_decode_sz = 0; 1011 u32 cur_offset = 0; 1012 1013 /* could be configured through the device tree */ 1014 flash->ahb_size = flash->spi->size; 1015 1016 if (priv->new_ver) { 1017 for (cs = 0; cs < ASPEED_SPI_MAX_CS; cs++) { 1018 reg_val = readl(&priv->regs->segment_addr[cs]); 1019 if (G6_SEGMENT_ADDR_END(reg_val) > G6_SEGMENT_ADDR_START(reg_val)) { 1020 decode_sz_arr[cs] = 1021 G6_SEGMENT_ADDR_END(reg_val) - G6_SEGMENT_ADDR_START(reg_val); 1022 } else { 1023 decode_sz_arr[cs] = 0; 1024 } 1025 } 1026 1027 decode_sz_arr[flash->cs] = flash->ahb_size; 1028 aspeed_g6_adjust_decode_sz(decode_sz_arr, flash->cs + 1); 1029 1030 for (cs = 0; cs < ASPEED_SPI_MAX_CS; cs++) 1031 total_decode_sz += decode_sz_arr[cs]; 1032 1033 if (total_decode_sz > priv->ahb_size) { 1034 printf("err: Total decoded size, 0x%x, is too large.\n", total_decode_sz); 1035 return -ENOMEM; 1036 } 1037 1038 for (cs = 0; cs < ASPEED_SPI_MAX_CS; cs++) { 1039 struct aspeed_spi_flash *flash = &priv->flashes[cs]; 1040 1041 flash->ahb_base = (void __iomem *)((u32)priv->ahb_base + cur_offset); 1042 1043 if (decode_sz_arr[cs] != 0) { 1044 seg_addr = G6_SEGMENT_ADDR_VALUE((u32)flash->ahb_base, 1045 (u32)flash->ahb_base + decode_sz_arr[cs]); 1046 } else { 1047 seg_addr = 0; 1048 } 1049 1050 writel(seg_addr, &priv->regs->segment_addr[cs]); 1051 flash->ahb_size = decode_sz_arr[cs]; 1052 cur_offset += decode_sz_arr[cs]; 1053 } 1054 } else { 1055 seg_addr = SEGMENT_ADDR_VALUE((u32)flash->ahb_base, 1056 (u32)flash->ahb_base + flash->ahb_size); 1057 writel(seg_addr, &priv->regs->segment_addr[flash->cs]); 1058 } 1059 1060 return 0; 1061 } 1062 1063 static int aspeed_spi_flash_init(struct aspeed_spi_priv *priv, 1064 struct aspeed_spi_flash *flash, 1065 struct udevice *dev) 1066 { 1067 int ret; 1068 struct spi_flash *spi_flash = dev_get_uclass_priv(dev); 1069 struct spi_slave *slave = dev_get_parent_priv(dev); 1070 struct udevice *bus = dev->parent; 1071 u32 read_hclk; 1072 1073 flash->spi = spi_flash; 1074 1075 /* 1076 * The flash device has not been probed yet. Initial transfers 1077 * to read the JEDEC of the device will use the initial 1078 * default settings of the registers. 1079 */ 1080 if (!spi_flash->name) 1081 return 0; 1082 1083 /* 1084 * The SPI flash device slave should not change, so initialize 1085 * it only once. 1086 */ 1087 if (flash->init) 1088 return 0; 1089 1090 debug("CS%u: init %s flags:%x size:%d page:%d sector:%d erase:%d", 1091 flash->cs, 1092 spi_flash->name, spi_flash->flags, spi_flash->size, 1093 spi_flash->page_size, spi_flash->sector_size, 1094 spi_flash->erase_size); 1095 debug(" cmds [ erase:%x read=%x write:%x ] dummy:%d, speed:%d\n", 1096 spi_flash->erase_opcode, 1097 spi_flash->read_opcode, spi_flash->program_opcode, 1098 spi_flash->read_dummy, slave->speed); 1099 1100 flash->ce_ctrl_user = CE_CTRL_USERMODE; 1101 flash->max_freq = slave->speed; 1102 1103 if(priv->new_ver) 1104 read_hclk = aspeed_g6_spi_hclk_divisor(priv, slave->speed); 1105 else 1106 read_hclk = aspeed_spi_hclk_divisor(priv, slave->speed); 1107 1108 switch(flash->spi->read_opcode) { 1109 case SPINOR_OP_READ_1_1_2: 1110 case SPINOR_OP_READ_1_1_2_4B: 1111 flash->read_iomode = CE_CTRL_IO_DUAL_DATA; 1112 break; 1113 case SPINOR_OP_READ_1_1_4: 1114 case SPINOR_OP_READ_1_1_4_4B: 1115 flash->read_iomode = CE_CTRL_IO_QUAD_DATA; 1116 break; 1117 case SPINOR_OP_READ_1_4_4: 1118 case SPINOR_OP_READ_1_4_4_4B: 1119 flash->read_iomode = CE_CTRL_IO_QUAD_ADDR_DATA; 1120 printf("need modify dummy for 3 bytes\n"); 1121 break; 1122 } 1123 1124 switch(flash->spi->program_opcode) { 1125 case SPINOR_OP_PP: 1126 case SPINOR_OP_PP_4B: 1127 flash->write_iomode = CE_CTRL_IO_SINGLE; 1128 break; 1129 case SPINOR_OP_PP_1_1_4: 1130 case SPINOR_OP_PP_1_1_4_4B: 1131 flash->write_iomode = CE_CTRL_IO_QUAD_DATA; 1132 break; 1133 case SPINOR_OP_PP_1_4_4: 1134 case SPINOR_OP_PP_1_4_4_4B: 1135 flash->write_iomode = CE_CTRL_IO_QUAD_ADDR_DATA; 1136 printf("need modify dummy for 3 bytes"); 1137 break; 1138 } 1139 1140 if(priv->new_ver) { 1141 flash->ce_ctrl_fread = CE_G6_CTRL_CLOCK_FREQ(read_hclk) | 1142 flash->read_iomode | 1143 CE_CTRL_CMD(flash->spi->read_opcode) | 1144 CE_CTRL_DUMMY((flash->spi->read_dummy/8)) | 1145 CE_CTRL_FREADMODE; 1146 flash->ce_ctrl_user |= CE_G6_CTRL_CLOCK_FREQ(read_hclk); 1147 } else { 1148 flash->ce_ctrl_fread = CE_CTRL_CLOCK_FREQ(read_hclk) | 1149 flash->read_iomode | 1150 CE_CTRL_CMD(flash->spi->read_opcode) | 1151 CE_CTRL_DUMMY((flash->spi->read_dummy/8)) | 1152 CE_CTRL_FREADMODE; 1153 } 1154 1155 if (flash->spi->addr_width == 4) 1156 writel(readl(&priv->regs->ctrl) | 0x11 << flash->cs, &priv->regs->ctrl); 1157 1158 debug("CS%u: USER mode 0x%08x FREAD mode 0x%08x\n", flash->cs, 1159 flash->ce_ctrl_user, flash->ce_ctrl_fread); 1160 1161 /* Set the CE Control Register default (FAST READ) */ 1162 writel(flash->ce_ctrl_fread, &priv->regs->ce_ctrl[flash->cs]); 1163 1164 /* Set Address Segment Register for direct AHB accesses */ 1165 ret = aspeed_spi_flash_set_segment(priv, flash); 1166 if (ret != 0) 1167 return ret; 1168 1169 /* 1170 * Set the Read Timing Compensation Register. This setting 1171 * applies to all devices. 1172 */ 1173 if (!dev_read_bool(bus, "timing-calibration-disabled")) { 1174 ret = aspeed_spi_timing_calibration(priv, flash); 1175 if (ret != 0) 1176 return ret; 1177 } 1178 1179 /* All done */ 1180 flash->init = true; 1181 1182 return 0; 1183 } 1184 1185 static int aspeed_spi_claim_bus(struct udevice *dev) 1186 { 1187 struct udevice *bus = dev->parent; 1188 struct aspeed_spi_priv *priv = dev_get_priv(bus); 1189 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev); 1190 struct aspeed_spi_flash *flash; 1191 1192 debug("%s: claim bus CS%u\n", bus->name, slave_plat->cs); 1193 1194 flash = aspeed_spi_get_flash(dev); 1195 if (!flash) 1196 return -ENODEV; 1197 1198 return aspeed_spi_flash_init(priv, flash, dev); 1199 } 1200 1201 static int aspeed_spi_release_bus(struct udevice *dev) 1202 { 1203 struct udevice *bus = dev->parent; 1204 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev); 1205 1206 debug("%s: release bus CS%u\n", bus->name, slave_plat->cs); 1207 1208 if (!aspeed_spi_get_flash(dev)) 1209 return -ENODEV; 1210 1211 return 0; 1212 } 1213 1214 static int aspeed_spi_set_mode(struct udevice *bus, uint mode) 1215 { 1216 debug("%s: setting mode to %x\n", bus->name, mode); 1217 1218 if (mode & (SPI_RX_QUAD | SPI_TX_QUAD)) { 1219 #ifndef CONFIG_ASPEED_AST2600 1220 pr_err("%s invalid QUAD IO mode\n", bus->name); 1221 return -EINVAL; 1222 #endif 1223 } 1224 1225 /* The CE Control Register is set in claim_bus() */ 1226 return 0; 1227 } 1228 1229 static int aspeed_spi_set_speed(struct udevice *bus, uint hz) 1230 { 1231 debug("%s: setting speed to %u\n", bus->name, hz); 1232 1233 /* The CE Control Register is set in claim_bus() */ 1234 return 0; 1235 } 1236 1237 static int aspeed_spi_count_flash_devices(struct udevice *bus) 1238 { 1239 ofnode node; 1240 int count = 0; 1241 1242 dev_for_each_subnode(node, bus) { 1243 if (ofnode_is_available(node) && 1244 ofnode_device_is_compatible(node, "spi-flash")) 1245 count++; 1246 } 1247 1248 return count; 1249 } 1250 1251 static int aspeed_spi_bind(struct udevice *bus) 1252 { 1253 debug("%s assigned req_seq=%d seq=%d\n", bus->name, bus->req_seq, 1254 bus->seq); 1255 1256 return 0; 1257 } 1258 1259 static int aspeed_spi_probe(struct udevice *bus) 1260 { 1261 struct resource res_regs, res_ahb; 1262 struct aspeed_spi_priv *priv = dev_get_priv(bus); 1263 struct clk hclk; 1264 int ret; 1265 1266 ret = dev_read_resource(bus, 0, &res_regs); 1267 if (ret < 0) 1268 return ret; 1269 1270 priv->regs = (void __iomem *)res_regs.start; 1271 1272 ret = dev_read_resource(bus, 1, &res_ahb); 1273 if (ret < 0) 1274 return ret; 1275 1276 priv->ahb_base = (void __iomem *)res_ahb.start; 1277 priv->ahb_size = res_ahb.end - res_ahb.start + 1; 1278 1279 ret = clk_get_by_index(bus, 0, &hclk); 1280 if (ret < 0) { 1281 pr_err("%s could not get clock: %d\n", bus->name, ret); 1282 return ret; 1283 } 1284 1285 priv->hclk_rate = clk_get_rate(&hclk); 1286 clk_free(&hclk); 1287 1288 priv->num_cs = dev_read_u32_default(bus, "num-cs", ASPEED_SPI_MAX_CS); 1289 1290 priv->flash_count = aspeed_spi_count_flash_devices(bus); 1291 if (priv->flash_count > priv->num_cs) { 1292 pr_err("%s has too many flash devices: %d\n", bus->name, 1293 priv->flash_count); 1294 return -EINVAL; 1295 } 1296 1297 if (!priv->flash_count) { 1298 pr_err("%s has no flash devices ?!\n", bus->name); 1299 return -ENODEV; 1300 } 1301 1302 if (device_is_compatible(bus, "aspeed,ast2600-fmc") || 1303 device_is_compatible(bus, "aspeed,ast2600-spi")) { 1304 priv->new_ver = 1; 1305 } 1306 1307 /* 1308 * There are some slight differences between the FMC and the 1309 * SPI controllers 1310 */ 1311 priv->is_fmc = dev_get_driver_data(bus); 1312 1313 ret = aspeed_spi_controller_init(priv); 1314 if (ret) 1315 return ret; 1316 1317 debug("%s probed regs=%p ahb_base=%p cs_num=%d seq=%d\n", 1318 bus->name, priv->regs, priv->ahb_base, priv->flash_count, bus->seq); 1319 1320 return 0; 1321 } 1322 1323 static const struct dm_spi_ops aspeed_spi_ops = { 1324 .claim_bus = aspeed_spi_claim_bus, 1325 .release_bus = aspeed_spi_release_bus, 1326 .set_mode = aspeed_spi_set_mode, 1327 .set_speed = aspeed_spi_set_speed, 1328 .xfer = aspeed_spi_xfer, 1329 }; 1330 1331 static const struct udevice_id aspeed_spi_ids[] = { 1332 { .compatible = "aspeed,ast2600-fmc", .data = 1 }, 1333 { .compatible = "aspeed,ast2600-spi", .data = 0 }, 1334 { .compatible = "aspeed,ast2500-fmc", .data = 1 }, 1335 { .compatible = "aspeed,ast2500-spi", .data = 0 }, 1336 { .compatible = "aspeed,ast2400-fmc", .data = 1 }, 1337 { } 1338 }; 1339 1340 U_BOOT_DRIVER(aspeed_spi) = { 1341 .name = "aspeed_spi", 1342 .id = UCLASS_SPI, 1343 .of_match = aspeed_spi_ids, 1344 .ops = &aspeed_spi_ops, 1345 .priv_auto_alloc_size = sizeof(struct aspeed_spi_priv), 1346 .child_pre_probe = aspeed_spi_child_pre_probe, 1347 .bind = aspeed_spi_bind, 1348 .probe = aspeed_spi_probe, 1349 }; 1350