intel-ixp4xx-eb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Intel IXP4xx Expansion Bus Controller
  4. * Copyright (C) 2021 Linaro Ltd.
  5. *
  6. * Author: Linus Walleij <[email protected]>
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/bits.h>
  10. #include <linux/err.h>
  11. #include <linux/init.h>
  12. #include <linux/log2.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regmap.h>
  19. #define IXP4XX_EXP_NUM_CS 8
  20. #define IXP4XX_EXP_TIMING_CS0 0x00
  21. #define IXP4XX_EXP_TIMING_CS1 0x04
  22. #define IXP4XX_EXP_TIMING_CS2 0x08
  23. #define IXP4XX_EXP_TIMING_CS3 0x0c
  24. #define IXP4XX_EXP_TIMING_CS4 0x10
  25. #define IXP4XX_EXP_TIMING_CS5 0x14
  26. #define IXP4XX_EXP_TIMING_CS6 0x18
  27. #define IXP4XX_EXP_TIMING_CS7 0x1c
  28. /* Bits inside each CS timing register */
  29. #define IXP4XX_EXP_TIMING_STRIDE 0x04
  30. #define IXP4XX_EXP_CS_EN BIT(31)
  31. #define IXP456_EXP_PAR_EN BIT(30) /* Only on IXP45x and IXP46x */
  32. #define IXP4XX_EXP_T1_MASK GENMASK(29, 28)
  33. #define IXP4XX_EXP_T1_SHIFT 28
  34. #define IXP4XX_EXP_T2_MASK GENMASK(27, 26)
  35. #define IXP4XX_EXP_T2_SHIFT 26
  36. #define IXP4XX_EXP_T3_MASK GENMASK(25, 22)
  37. #define IXP4XX_EXP_T3_SHIFT 22
  38. #define IXP4XX_EXP_T4_MASK GENMASK(21, 20)
  39. #define IXP4XX_EXP_T4_SHIFT 20
  40. #define IXP4XX_EXP_T5_MASK GENMASK(19, 16)
  41. #define IXP4XX_EXP_T5_SHIFT 16
  42. #define IXP4XX_EXP_CYC_TYPE_MASK GENMASK(15, 14)
  43. #define IXP4XX_EXP_CYC_TYPE_SHIFT 14
  44. #define IXP4XX_EXP_SIZE_MASK GENMASK(13, 10)
  45. #define IXP4XX_EXP_SIZE_SHIFT 10
  46. #define IXP4XX_EXP_CNFG_0 BIT(9) /* Always zero */
  47. #define IXP43X_EXP_SYNC_INTEL BIT(8) /* Only on IXP43x */
  48. #define IXP43X_EXP_EXP_CHIP BIT(7) /* Only on IXP43x, dangerous to touch on IXP42x */
  49. #define IXP4XX_EXP_BYTE_RD16 BIT(6)
  50. #define IXP4XX_EXP_HRDY_POL BIT(5) /* Only on IXP42x */
  51. #define IXP4XX_EXP_MUX_EN BIT(4)
  52. #define IXP4XX_EXP_SPLT_EN BIT(3)
  53. #define IXP4XX_EXP_WORD BIT(2) /* Always zero */
  54. #define IXP4XX_EXP_WR_EN BIT(1)
  55. #define IXP4XX_EXP_BYTE_EN BIT(0)
  56. #define IXP4XX_EXP_CNFG0 0x20
  57. #define IXP4XX_EXP_CNFG0_MEM_MAP BIT(31)
  58. #define IXP4XX_EXP_CNFG1 0x24
  59. #define IXP4XX_EXP_BOOT_BASE 0x00000000
  60. #define IXP4XX_EXP_NORMAL_BASE 0x50000000
  61. #define IXP4XX_EXP_STRIDE 0x01000000
  62. /* Fuses on the IXP43x */
  63. #define IXP43X_EXP_UNIT_FUSE_RESET 0x28
  64. #define IXP43x_EXP_FUSE_SPEED_MASK GENMASK(23, 22)
  65. /* Number of device tree values in "reg" */
  66. #define IXP4XX_OF_REG_SIZE 3
  67. struct ixp4xx_eb {
  68. struct device *dev;
  69. struct regmap *rmap;
  70. u32 bus_base;
  71. bool is_42x;
  72. bool is_43x;
  73. };
  74. struct ixp4xx_exp_tim_prop {
  75. const char *prop;
  76. u32 max;
  77. u32 mask;
  78. u16 shift;
  79. };
  80. static const struct ixp4xx_exp_tim_prop ixp4xx_exp_tim_props[] = {
  81. {
  82. .prop = "intel,ixp4xx-eb-t1",
  83. .max = 3,
  84. .mask = IXP4XX_EXP_T1_MASK,
  85. .shift = IXP4XX_EXP_T1_SHIFT,
  86. },
  87. {
  88. .prop = "intel,ixp4xx-eb-t2",
  89. .max = 3,
  90. .mask = IXP4XX_EXP_T2_MASK,
  91. .shift = IXP4XX_EXP_T2_SHIFT,
  92. },
  93. {
  94. .prop = "intel,ixp4xx-eb-t3",
  95. .max = 15,
  96. .mask = IXP4XX_EXP_T3_MASK,
  97. .shift = IXP4XX_EXP_T3_SHIFT,
  98. },
  99. {
  100. .prop = "intel,ixp4xx-eb-t4",
  101. .max = 3,
  102. .mask = IXP4XX_EXP_T4_MASK,
  103. .shift = IXP4XX_EXP_T4_SHIFT,
  104. },
  105. {
  106. .prop = "intel,ixp4xx-eb-t5",
  107. .max = 15,
  108. .mask = IXP4XX_EXP_T5_MASK,
  109. .shift = IXP4XX_EXP_T5_SHIFT,
  110. },
  111. {
  112. .prop = "intel,ixp4xx-eb-byte-access-on-halfword",
  113. .max = 1,
  114. .mask = IXP4XX_EXP_BYTE_RD16,
  115. },
  116. {
  117. .prop = "intel,ixp4xx-eb-hpi-hrdy-pol-high",
  118. .max = 1,
  119. .mask = IXP4XX_EXP_HRDY_POL,
  120. },
  121. {
  122. .prop = "intel,ixp4xx-eb-mux-address-and-data",
  123. .max = 1,
  124. .mask = IXP4XX_EXP_MUX_EN,
  125. },
  126. {
  127. .prop = "intel,ixp4xx-eb-ahb-split-transfers",
  128. .max = 1,
  129. .mask = IXP4XX_EXP_SPLT_EN,
  130. },
  131. {
  132. .prop = "intel,ixp4xx-eb-write-enable",
  133. .max = 1,
  134. .mask = IXP4XX_EXP_WR_EN,
  135. },
  136. {
  137. .prop = "intel,ixp4xx-eb-byte-access",
  138. .max = 1,
  139. .mask = IXP4XX_EXP_BYTE_EN,
  140. },
  141. };
  142. static void ixp4xx_exp_setup_chipselect(struct ixp4xx_eb *eb,
  143. struct device_node *np,
  144. u32 cs_index,
  145. u32 cs_size)
  146. {
  147. u32 cs_cfg;
  148. u32 val;
  149. u32 cur_cssize;
  150. u32 cs_order;
  151. int ret;
  152. int i;
  153. if (eb->is_42x && (cs_index > 7)) {
  154. dev_err(eb->dev,
  155. "invalid chipselect %u, we only support 0-7\n",
  156. cs_index);
  157. return;
  158. }
  159. if (eb->is_43x && (cs_index > 3)) {
  160. dev_err(eb->dev,
  161. "invalid chipselect %u, we only support 0-3\n",
  162. cs_index);
  163. return;
  164. }
  165. /* Several chip selects can be joined into one device */
  166. if (cs_size > IXP4XX_EXP_STRIDE)
  167. cur_cssize = IXP4XX_EXP_STRIDE;
  168. else
  169. cur_cssize = cs_size;
  170. /*
  171. * The following will read/modify/write the configuration for one
  172. * chipselect, attempting to leave the boot defaults in place unless
  173. * something is explicitly defined.
  174. */
  175. regmap_read(eb->rmap, IXP4XX_EXP_TIMING_CS0 +
  176. IXP4XX_EXP_TIMING_STRIDE * cs_index, &cs_cfg);
  177. dev_info(eb->dev, "CS%d at %#08x, size %#08x, config before: %#08x\n",
  178. cs_index, eb->bus_base + IXP4XX_EXP_STRIDE * cs_index,
  179. cur_cssize, cs_cfg);
  180. /* Size set-up first align to 2^9 .. 2^24 */
  181. cur_cssize = roundup_pow_of_two(cur_cssize);
  182. if (cur_cssize < 512)
  183. cur_cssize = 512;
  184. cs_order = ilog2(cur_cssize);
  185. if (cs_order < 9 || cs_order > 24) {
  186. dev_err(eb->dev, "illegal size order %d\n", cs_order);
  187. return;
  188. }
  189. dev_dbg(eb->dev, "CS%d size order: %d\n", cs_index, cs_order);
  190. cs_cfg &= ~(IXP4XX_EXP_SIZE_MASK);
  191. cs_cfg |= ((cs_order - 9) << IXP4XX_EXP_SIZE_SHIFT);
  192. for (i = 0; i < ARRAY_SIZE(ixp4xx_exp_tim_props); i++) {
  193. const struct ixp4xx_exp_tim_prop *ip = &ixp4xx_exp_tim_props[i];
  194. /* All are regular u32 values */
  195. ret = of_property_read_u32(np, ip->prop, &val);
  196. if (ret)
  197. continue;
  198. /* Handle bools (single bits) first */
  199. if (ip->max == 1) {
  200. if (val)
  201. cs_cfg |= ip->mask;
  202. else
  203. cs_cfg &= ~ip->mask;
  204. dev_info(eb->dev, "CS%d %s %s\n", cs_index,
  205. val ? "enabled" : "disabled",
  206. ip->prop);
  207. continue;
  208. }
  209. if (val > ip->max) {
  210. dev_err(eb->dev,
  211. "CS%d too high value for %s: %u, capped at %u\n",
  212. cs_index, ip->prop, val, ip->max);
  213. val = ip->max;
  214. }
  215. /* This assumes max value fills all the assigned bits (and it does) */
  216. cs_cfg &= ~ip->mask;
  217. cs_cfg |= (val << ip->shift);
  218. dev_info(eb->dev, "CS%d set %s to %u\n", cs_index, ip->prop, val);
  219. }
  220. ret = of_property_read_u32(np, "intel,ixp4xx-eb-cycle-type", &val);
  221. if (!ret) {
  222. if (val > 3) {
  223. dev_err(eb->dev, "illegal cycle type %d\n", val);
  224. return;
  225. }
  226. dev_info(eb->dev, "CS%d set cycle type %d\n", cs_index, val);
  227. cs_cfg &= ~IXP4XX_EXP_CYC_TYPE_MASK;
  228. cs_cfg |= val << IXP4XX_EXP_CYC_TYPE_SHIFT;
  229. }
  230. if (eb->is_43x) {
  231. /* Should always be zero */
  232. cs_cfg &= ~IXP4XX_EXP_WORD;
  233. /*
  234. * This bit for Intel strata flash is currently unused, but let's
  235. * report it if we find one.
  236. */
  237. if (cs_cfg & IXP43X_EXP_SYNC_INTEL)
  238. dev_info(eb->dev, "claims to be Intel strata flash\n");
  239. }
  240. cs_cfg |= IXP4XX_EXP_CS_EN;
  241. regmap_write(eb->rmap,
  242. IXP4XX_EXP_TIMING_CS0 + IXP4XX_EXP_TIMING_STRIDE * cs_index,
  243. cs_cfg);
  244. dev_info(eb->dev, "CS%d wrote %#08x into CS config\n", cs_index, cs_cfg);
  245. /*
  246. * If several chip selects are joined together into one big
  247. * device area, we call ourselves recursively for each successive
  248. * chip select. For a 32MB flash chip this results in two calls
  249. * for example.
  250. */
  251. if (cs_size > IXP4XX_EXP_STRIDE)
  252. ixp4xx_exp_setup_chipselect(eb, np,
  253. cs_index + 1,
  254. cs_size - IXP4XX_EXP_STRIDE);
  255. }
  256. static void ixp4xx_exp_setup_child(struct ixp4xx_eb *eb,
  257. struct device_node *np)
  258. {
  259. u32 cs_sizes[IXP4XX_EXP_NUM_CS];
  260. int num_regs;
  261. u32 csindex;
  262. u32 cssize;
  263. int ret;
  264. int i;
  265. num_regs = of_property_count_elems_of_size(np, "reg", IXP4XX_OF_REG_SIZE);
  266. if (num_regs <= 0)
  267. return;
  268. dev_dbg(eb->dev, "child %s has %d register sets\n",
  269. of_node_full_name(np), num_regs);
  270. for (csindex = 0; csindex < IXP4XX_EXP_NUM_CS; csindex++)
  271. cs_sizes[csindex] = 0;
  272. for (i = 0; i < num_regs; i++) {
  273. u32 rbase, rsize;
  274. ret = of_property_read_u32_index(np, "reg",
  275. i * IXP4XX_OF_REG_SIZE, &csindex);
  276. if (ret)
  277. break;
  278. ret = of_property_read_u32_index(np, "reg",
  279. i * IXP4XX_OF_REG_SIZE + 1, &rbase);
  280. if (ret)
  281. break;
  282. ret = of_property_read_u32_index(np, "reg",
  283. i * IXP4XX_OF_REG_SIZE + 2, &rsize);
  284. if (ret)
  285. break;
  286. if (csindex >= IXP4XX_EXP_NUM_CS) {
  287. dev_err(eb->dev, "illegal CS %d\n", csindex);
  288. continue;
  289. }
  290. /*
  291. * The memory window always starts from CS base so we need to add
  292. * the start and size to get to the size from the start of the CS
  293. * base. For example if CS0 is at 0x50000000 and the reg is
  294. * <0 0xe40000 0x40000> the size is e80000.
  295. *
  296. * Roof this if we have several regs setting the same CS.
  297. */
  298. cssize = rbase + rsize;
  299. dev_dbg(eb->dev, "CS%d size %#08x\n", csindex, cssize);
  300. if (cs_sizes[csindex] < cssize)
  301. cs_sizes[csindex] = cssize;
  302. }
  303. for (csindex = 0; csindex < IXP4XX_EXP_NUM_CS; csindex++) {
  304. cssize = cs_sizes[csindex];
  305. if (!cssize)
  306. continue;
  307. /* Just this one, so set it up and return */
  308. ixp4xx_exp_setup_chipselect(eb, np, csindex, cssize);
  309. }
  310. }
  311. static int ixp4xx_exp_probe(struct platform_device *pdev)
  312. {
  313. struct device *dev = &pdev->dev;
  314. struct device_node *np = dev->of_node;
  315. struct ixp4xx_eb *eb;
  316. struct device_node *child;
  317. bool have_children = false;
  318. u32 val;
  319. int ret;
  320. eb = devm_kzalloc(dev, sizeof(*eb), GFP_KERNEL);
  321. if (!eb)
  322. return -ENOMEM;
  323. eb->dev = dev;
  324. eb->is_42x = of_device_is_compatible(np, "intel,ixp42x-expansion-bus-controller");
  325. eb->is_43x = of_device_is_compatible(np, "intel,ixp43x-expansion-bus-controller");
  326. eb->rmap = syscon_node_to_regmap(np);
  327. if (IS_ERR(eb->rmap))
  328. return dev_err_probe(dev, PTR_ERR(eb->rmap), "no regmap\n");
  329. /* We check that the regmap work only on first read */
  330. ret = regmap_read(eb->rmap, IXP4XX_EXP_CNFG0, &val);
  331. if (ret)
  332. return dev_err_probe(dev, ret, "cannot read regmap\n");
  333. if (val & IXP4XX_EXP_CNFG0_MEM_MAP)
  334. eb->bus_base = IXP4XX_EXP_BOOT_BASE;
  335. else
  336. eb->bus_base = IXP4XX_EXP_NORMAL_BASE;
  337. dev_info(dev, "expansion bus at %08x\n", eb->bus_base);
  338. if (eb->is_43x) {
  339. /* Check some fuses */
  340. regmap_read(eb->rmap, IXP43X_EXP_UNIT_FUSE_RESET, &val);
  341. switch (FIELD_GET(IXP43x_EXP_FUSE_SPEED_MASK, val)) {
  342. case 0:
  343. dev_info(dev, "IXP43x at 533 MHz\n");
  344. break;
  345. case 1:
  346. dev_info(dev, "IXP43x at 400 MHz\n");
  347. break;
  348. case 2:
  349. dev_info(dev, "IXP43x at 667 MHz\n");
  350. break;
  351. default:
  352. dev_info(dev, "IXP43x unknown speed\n");
  353. break;
  354. }
  355. }
  356. /* Walk over the child nodes and see what chipselects we use */
  357. for_each_available_child_of_node(np, child) {
  358. ixp4xx_exp_setup_child(eb, child);
  359. /* We have at least one child */
  360. have_children = true;
  361. }
  362. if (have_children)
  363. return of_platform_default_populate(np, NULL, dev);
  364. return 0;
  365. }
  366. static const struct of_device_id ixp4xx_exp_of_match[] = {
  367. { .compatible = "intel,ixp42x-expansion-bus-controller", },
  368. { .compatible = "intel,ixp43x-expansion-bus-controller", },
  369. { .compatible = "intel,ixp45x-expansion-bus-controller", },
  370. { .compatible = "intel,ixp46x-expansion-bus-controller", },
  371. { }
  372. };
  373. static struct platform_driver ixp4xx_exp_driver = {
  374. .probe = ixp4xx_exp_probe,
  375. .driver = {
  376. .name = "intel-extbus",
  377. .of_match_table = ixp4xx_exp_of_match,
  378. },
  379. };
  380. module_platform_driver(ixp4xx_exp_driver);
  381. MODULE_AUTHOR("Linus Walleij <[email protected]>");
  382. MODULE_DESCRIPTION("Intel IXP4xx external bus driver");
  383. MODULE_LICENSE("GPL");