atmel-ebi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * EBI driver for Atmel chips
  3. * inspired by the fsl weim bus driver
  4. *
  5. * Copyright (C) 2013 Jean-Jacques Hiblot <jjhiblot@traphandler.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/io.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/mfd/syscon/atmel-matrix.h>
  15. #include <linux/mfd/syscon/atmel-smc.h>
  16. #include <linux/init.h>
  17. #include <linux/of_device.h>
  18. #include <linux/regmap.h>
  19. struct at91sam9_smc_timings {
  20. u32 ncs_rd_setup_ns;
  21. u32 nrd_setup_ns;
  22. u32 ncs_wr_setup_ns;
  23. u32 nwe_setup_ns;
  24. u32 ncs_rd_pulse_ns;
  25. u32 nrd_pulse_ns;
  26. u32 ncs_wr_pulse_ns;
  27. u32 nwe_pulse_ns;
  28. u32 nrd_cycle_ns;
  29. u32 nwe_cycle_ns;
  30. u32 tdf_ns;
  31. };
  32. struct at91sam9_smc_generic_fields {
  33. struct regmap_field *setup;
  34. struct regmap_field *pulse;
  35. struct regmap_field *cycle;
  36. struct regmap_field *mode;
  37. };
  38. struct at91sam9_ebi_dev_config {
  39. struct at91sam9_smc_timings timings;
  40. u32 mode;
  41. };
  42. struct at91_ebi_dev_config {
  43. int cs;
  44. union {
  45. struct at91sam9_ebi_dev_config sam9;
  46. };
  47. };
  48. struct at91_ebi;
  49. struct at91_ebi_dev {
  50. struct list_head node;
  51. struct at91_ebi *ebi;
  52. u32 mode;
  53. int numcs;
  54. struct at91_ebi_dev_config configs[];
  55. };
  56. struct at91_ebi_caps {
  57. unsigned int available_cs;
  58. const struct reg_field *ebi_csa;
  59. void (*get_config)(struct at91_ebi_dev *ebid,
  60. struct at91_ebi_dev_config *conf);
  61. int (*xlate_config)(struct at91_ebi_dev *ebid,
  62. struct device_node *configs_np,
  63. struct at91_ebi_dev_config *conf);
  64. int (*apply_config)(struct at91_ebi_dev *ebid,
  65. struct at91_ebi_dev_config *conf);
  66. int (*init)(struct at91_ebi *ebi);
  67. };
  68. struct at91_ebi {
  69. struct clk *clk;
  70. struct regmap *smc;
  71. struct regmap *matrix;
  72. struct regmap_field *ebi_csa;
  73. struct device *dev;
  74. const struct at91_ebi_caps *caps;
  75. struct list_head devs;
  76. union {
  77. struct at91sam9_smc_generic_fields sam9;
  78. };
  79. };
  80. static void at91sam9_ebi_get_config(struct at91_ebi_dev *ebid,
  81. struct at91_ebi_dev_config *conf)
  82. {
  83. struct at91sam9_smc_generic_fields *fields = &ebid->ebi->sam9;
  84. unsigned int clk_period = NSEC_PER_SEC / clk_get_rate(ebid->ebi->clk);
  85. struct at91sam9_ebi_dev_config *config = &conf->sam9;
  86. struct at91sam9_smc_timings *timings = &config->timings;
  87. unsigned int val;
  88. regmap_fields_read(fields->mode, conf->cs, &val);
  89. config->mode = val & ~AT91_SMC_TDF;
  90. val = (val & AT91_SMC_TDF) >> 16;
  91. timings->tdf_ns = clk_period * val;
  92. regmap_fields_read(fields->setup, conf->cs, &val);
  93. timings->ncs_rd_setup_ns = (val >> 24) & 0x1f;
  94. timings->ncs_rd_setup_ns += ((val >> 29) & 0x1) * 128;
  95. timings->ncs_rd_setup_ns *= clk_period;
  96. timings->nrd_setup_ns = (val >> 16) & 0x1f;
  97. timings->nrd_setup_ns += ((val >> 21) & 0x1) * 128;
  98. timings->nrd_setup_ns *= clk_period;
  99. timings->ncs_wr_setup_ns = (val >> 8) & 0x1f;
  100. timings->ncs_wr_setup_ns += ((val >> 13) & 0x1) * 128;
  101. timings->ncs_wr_setup_ns *= clk_period;
  102. timings->nwe_setup_ns = val & 0x1f;
  103. timings->nwe_setup_ns += ((val >> 5) & 0x1) * 128;
  104. timings->nwe_setup_ns *= clk_period;
  105. regmap_fields_read(fields->pulse, conf->cs, &val);
  106. timings->ncs_rd_pulse_ns = (val >> 24) & 0x3f;
  107. timings->ncs_rd_pulse_ns += ((val >> 30) & 0x1) * 256;
  108. timings->ncs_rd_pulse_ns *= clk_period;
  109. timings->nrd_pulse_ns = (val >> 16) & 0x3f;
  110. timings->nrd_pulse_ns += ((val >> 22) & 0x1) * 256;
  111. timings->nrd_pulse_ns *= clk_period;
  112. timings->ncs_wr_pulse_ns = (val >> 8) & 0x3f;
  113. timings->ncs_wr_pulse_ns += ((val >> 14) & 0x1) * 256;
  114. timings->ncs_wr_pulse_ns *= clk_period;
  115. timings->nwe_pulse_ns = val & 0x3f;
  116. timings->nwe_pulse_ns += ((val >> 6) & 0x1) * 256;
  117. timings->nwe_pulse_ns *= clk_period;
  118. regmap_fields_read(fields->cycle, conf->cs, &val);
  119. timings->nrd_cycle_ns = (val >> 16) & 0x7f;
  120. timings->nrd_cycle_ns += ((val >> 23) & 0x3) * 256;
  121. timings->nrd_cycle_ns *= clk_period;
  122. timings->nwe_cycle_ns = val & 0x7f;
  123. timings->nwe_cycle_ns += ((val >> 7) & 0x3) * 256;
  124. timings->nwe_cycle_ns *= clk_period;
  125. }
  126. static int at91_xlate_timing(struct device_node *np, const char *prop,
  127. u32 *val, bool *required)
  128. {
  129. if (!of_property_read_u32(np, prop, val)) {
  130. *required = true;
  131. return 0;
  132. }
  133. if (*required)
  134. return -EINVAL;
  135. return 0;
  136. }
  137. static int at91sam9_smc_xslate_timings(struct at91_ebi_dev *ebid,
  138. struct device_node *np,
  139. struct at91sam9_smc_timings *timings,
  140. bool *required)
  141. {
  142. int ret;
  143. ret = at91_xlate_timing(np, "atmel,smc-ncs-rd-setup-ns",
  144. &timings->ncs_rd_setup_ns, required);
  145. if (ret)
  146. goto out;
  147. ret = at91_xlate_timing(np, "atmel,smc-nrd-setup-ns",
  148. &timings->nrd_setup_ns, required);
  149. if (ret)
  150. goto out;
  151. ret = at91_xlate_timing(np, "atmel,smc-ncs-wr-setup-ns",
  152. &timings->ncs_wr_setup_ns, required);
  153. if (ret)
  154. goto out;
  155. ret = at91_xlate_timing(np, "atmel,smc-nwe-setup-ns",
  156. &timings->nwe_setup_ns, required);
  157. if (ret)
  158. goto out;
  159. ret = at91_xlate_timing(np, "atmel,smc-ncs-rd-pulse-ns",
  160. &timings->ncs_rd_pulse_ns, required);
  161. if (ret)
  162. goto out;
  163. ret = at91_xlate_timing(np, "atmel,smc-nrd-pulse-ns",
  164. &timings->nrd_pulse_ns, required);
  165. if (ret)
  166. goto out;
  167. ret = at91_xlate_timing(np, "atmel,smc-ncs-wr-pulse-ns",
  168. &timings->ncs_wr_pulse_ns, required);
  169. if (ret)
  170. goto out;
  171. ret = at91_xlate_timing(np, "atmel,smc-nwe-pulse-ns",
  172. &timings->nwe_pulse_ns, required);
  173. if (ret)
  174. goto out;
  175. ret = at91_xlate_timing(np, "atmel,smc-nwe-cycle-ns",
  176. &timings->nwe_cycle_ns, required);
  177. if (ret)
  178. goto out;
  179. ret = at91_xlate_timing(np, "atmel,smc-nrd-cycle-ns",
  180. &timings->nrd_cycle_ns, required);
  181. if (ret)
  182. goto out;
  183. ret = at91_xlate_timing(np, "atmel,smc-tdf-ns",
  184. &timings->tdf_ns, required);
  185. out:
  186. if (ret)
  187. dev_err(ebid->ebi->dev,
  188. "missing or invalid timings definition in %s",
  189. np->full_name);
  190. return ret;
  191. }
  192. static int at91sam9_ebi_xslate_config(struct at91_ebi_dev *ebid,
  193. struct device_node *np,
  194. struct at91_ebi_dev_config *conf)
  195. {
  196. struct at91sam9_ebi_dev_config *config = &conf->sam9;
  197. bool required = false;
  198. const char *tmp_str;
  199. u32 tmp;
  200. int ret;
  201. ret = of_property_read_u32(np, "atmel,smc-bus-width", &tmp);
  202. if (!ret) {
  203. switch (tmp) {
  204. case 8:
  205. config->mode |= AT91_SMC_DBW_8;
  206. break;
  207. case 16:
  208. config->mode |= AT91_SMC_DBW_16;
  209. break;
  210. case 32:
  211. config->mode |= AT91_SMC_DBW_32;
  212. break;
  213. default:
  214. return -EINVAL;
  215. }
  216. required = true;
  217. }
  218. if (of_property_read_bool(np, "atmel,smc-tdf-optimized")) {
  219. config->mode |= AT91_SMC_TDFMODE_OPTIMIZED;
  220. required = true;
  221. }
  222. tmp_str = NULL;
  223. of_property_read_string(np, "atmel,smc-byte-access-type", &tmp_str);
  224. if (tmp_str && !strcmp(tmp_str, "write")) {
  225. config->mode |= AT91_SMC_BAT_WRITE;
  226. required = true;
  227. }
  228. tmp_str = NULL;
  229. of_property_read_string(np, "atmel,smc-read-mode", &tmp_str);
  230. if (tmp_str && !strcmp(tmp_str, "nrd")) {
  231. config->mode |= AT91_SMC_READMODE_NRD;
  232. required = true;
  233. }
  234. tmp_str = NULL;
  235. of_property_read_string(np, "atmel,smc-write-mode", &tmp_str);
  236. if (tmp_str && !strcmp(tmp_str, "nwe")) {
  237. config->mode |= AT91_SMC_WRITEMODE_NWE;
  238. required = true;
  239. }
  240. tmp_str = NULL;
  241. of_property_read_string(np, "atmel,smc-exnw-mode", &tmp_str);
  242. if (tmp_str) {
  243. if (!strcmp(tmp_str, "frozen"))
  244. config->mode |= AT91_SMC_EXNWMODE_FROZEN;
  245. else if (!strcmp(tmp_str, "ready"))
  246. config->mode |= AT91_SMC_EXNWMODE_READY;
  247. else if (strcmp(tmp_str, "disabled"))
  248. return -EINVAL;
  249. required = true;
  250. }
  251. ret = of_property_read_u32(np, "atmel,smc-page-mode", &tmp);
  252. if (!ret) {
  253. switch (tmp) {
  254. case 4:
  255. config->mode |= AT91_SMC_PS_4;
  256. break;
  257. case 8:
  258. config->mode |= AT91_SMC_PS_8;
  259. break;
  260. case 16:
  261. config->mode |= AT91_SMC_PS_16;
  262. break;
  263. case 32:
  264. config->mode |= AT91_SMC_PS_32;
  265. break;
  266. default:
  267. return -EINVAL;
  268. }
  269. config->mode |= AT91_SMC_PMEN;
  270. required = true;
  271. }
  272. ret = at91sam9_smc_xslate_timings(ebid, np, &config->timings,
  273. &required);
  274. if (ret)
  275. return ret;
  276. return required;
  277. }
  278. static int at91sam9_ebi_apply_config(struct at91_ebi_dev *ebid,
  279. struct at91_ebi_dev_config *conf)
  280. {
  281. unsigned int clk_rate = clk_get_rate(ebid->ebi->clk);
  282. unsigned int clk_period = NSEC_PER_SEC / clk_rate;
  283. struct at91sam9_ebi_dev_config *config = &conf->sam9;
  284. struct at91sam9_smc_timings *timings = &config->timings;
  285. struct at91sam9_smc_generic_fields *fields = &ebid->ebi->sam9;
  286. u32 coded_val;
  287. u32 val;
  288. coded_val = at91sam9_smc_setup_ns_to_cycles(clk_rate,
  289. timings->ncs_rd_setup_ns);
  290. val = AT91SAM9_SMC_NCS_NRDSETUP(coded_val);
  291. coded_val = at91sam9_smc_setup_ns_to_cycles(clk_rate,
  292. timings->nrd_setup_ns);
  293. val |= AT91SAM9_SMC_NRDSETUP(coded_val);
  294. coded_val = at91sam9_smc_setup_ns_to_cycles(clk_rate,
  295. timings->ncs_wr_setup_ns);
  296. val |= AT91SAM9_SMC_NCS_WRSETUP(coded_val);
  297. coded_val = at91sam9_smc_setup_ns_to_cycles(clk_rate,
  298. timings->nwe_setup_ns);
  299. val |= AT91SAM9_SMC_NWESETUP(coded_val);
  300. regmap_fields_write(fields->setup, conf->cs, val);
  301. coded_val = at91sam9_smc_pulse_ns_to_cycles(clk_rate,
  302. timings->ncs_rd_pulse_ns);
  303. val = AT91SAM9_SMC_NCS_NRDPULSE(coded_val);
  304. coded_val = at91sam9_smc_pulse_ns_to_cycles(clk_rate,
  305. timings->nrd_pulse_ns);
  306. val |= AT91SAM9_SMC_NRDPULSE(coded_val);
  307. coded_val = at91sam9_smc_pulse_ns_to_cycles(clk_rate,
  308. timings->ncs_wr_pulse_ns);
  309. val |= AT91SAM9_SMC_NCS_WRPULSE(coded_val);
  310. coded_val = at91sam9_smc_pulse_ns_to_cycles(clk_rate,
  311. timings->nwe_pulse_ns);
  312. val |= AT91SAM9_SMC_NWEPULSE(coded_val);
  313. regmap_fields_write(fields->pulse, conf->cs, val);
  314. coded_val = at91sam9_smc_cycle_ns_to_cycles(clk_rate,
  315. timings->nrd_cycle_ns);
  316. val = AT91SAM9_SMC_NRDCYCLE(coded_val);
  317. coded_val = at91sam9_smc_cycle_ns_to_cycles(clk_rate,
  318. timings->nwe_cycle_ns);
  319. val |= AT91SAM9_SMC_NWECYCLE(coded_val);
  320. regmap_fields_write(fields->cycle, conf->cs, val);
  321. val = DIV_ROUND_UP(timings->tdf_ns, clk_period);
  322. if (val > AT91_SMC_TDF_MAX)
  323. val = AT91_SMC_TDF_MAX;
  324. regmap_fields_write(fields->mode, conf->cs,
  325. config->mode | AT91_SMC_TDF_(val));
  326. return 0;
  327. }
  328. static int at91sam9_ebi_init(struct at91_ebi *ebi)
  329. {
  330. struct at91sam9_smc_generic_fields *fields = &ebi->sam9;
  331. struct reg_field field = REG_FIELD(0, 0, 31);
  332. field.id_size = fls(ebi->caps->available_cs);
  333. field.id_offset = AT91SAM9_SMC_GENERIC_BLK_SZ;
  334. field.reg = AT91SAM9_SMC_SETUP(AT91SAM9_SMC_GENERIC);
  335. fields->setup = devm_regmap_field_alloc(ebi->dev, ebi->smc, field);
  336. if (IS_ERR(fields->setup))
  337. return PTR_ERR(fields->setup);
  338. field.reg = AT91SAM9_SMC_PULSE(AT91SAM9_SMC_GENERIC);
  339. fields->pulse = devm_regmap_field_alloc(ebi->dev, ebi->smc, field);
  340. if (IS_ERR(fields->pulse))
  341. return PTR_ERR(fields->pulse);
  342. field.reg = AT91SAM9_SMC_CYCLE(AT91SAM9_SMC_GENERIC);
  343. fields->cycle = devm_regmap_field_alloc(ebi->dev, ebi->smc, field);
  344. if (IS_ERR(fields->cycle))
  345. return PTR_ERR(fields->cycle);
  346. field.reg = AT91SAM9_SMC_MODE(AT91SAM9_SMC_GENERIC);
  347. fields->mode = devm_regmap_field_alloc(ebi->dev, ebi->smc, field);
  348. return PTR_ERR_OR_ZERO(fields->mode);
  349. }
  350. static int sama5d3_ebi_init(struct at91_ebi *ebi)
  351. {
  352. struct at91sam9_smc_generic_fields *fields = &ebi->sam9;
  353. struct reg_field field = REG_FIELD(0, 0, 31);
  354. field.id_size = fls(ebi->caps->available_cs);
  355. field.id_offset = SAMA5_SMC_GENERIC_BLK_SZ;
  356. field.reg = AT91SAM9_SMC_SETUP(SAMA5_SMC_GENERIC);
  357. fields->setup = devm_regmap_field_alloc(ebi->dev, ebi->smc, field);
  358. if (IS_ERR(fields->setup))
  359. return PTR_ERR(fields->setup);
  360. field.reg = AT91SAM9_SMC_PULSE(SAMA5_SMC_GENERIC);
  361. fields->pulse = devm_regmap_field_alloc(ebi->dev, ebi->smc, field);
  362. if (IS_ERR(fields->pulse))
  363. return PTR_ERR(fields->pulse);
  364. field.reg = AT91SAM9_SMC_CYCLE(SAMA5_SMC_GENERIC);
  365. fields->cycle = devm_regmap_field_alloc(ebi->dev, ebi->smc, field);
  366. if (IS_ERR(fields->cycle))
  367. return PTR_ERR(fields->cycle);
  368. field.reg = SAMA5_SMC_MODE(SAMA5_SMC_GENERIC);
  369. fields->mode = devm_regmap_field_alloc(ebi->dev, ebi->smc, field);
  370. return PTR_ERR_OR_ZERO(fields->mode);
  371. }
  372. static int at91_ebi_dev_setup(struct at91_ebi *ebi, struct device_node *np,
  373. int reg_cells)
  374. {
  375. const struct at91_ebi_caps *caps = ebi->caps;
  376. struct at91_ebi_dev_config conf = { };
  377. struct device *dev = ebi->dev;
  378. struct at91_ebi_dev *ebid;
  379. int ret, numcs = 0, i;
  380. bool apply = false;
  381. numcs = of_property_count_elems_of_size(np, "reg",
  382. reg_cells * sizeof(u32));
  383. if (numcs <= 0) {
  384. dev_err(dev, "invalid reg property in %s\n", np->full_name);
  385. return -EINVAL;
  386. }
  387. ebid = devm_kzalloc(ebi->dev,
  388. sizeof(*ebid) + (numcs * sizeof(*ebid->configs)),
  389. GFP_KERNEL);
  390. if (!ebid)
  391. return -ENOMEM;
  392. ebid->ebi = ebi;
  393. ret = caps->xlate_config(ebid, np, &conf);
  394. if (ret < 0)
  395. return ret;
  396. else if (ret)
  397. apply = true;
  398. for (i = 0; i < numcs; i++) {
  399. u32 cs;
  400. ret = of_property_read_u32_index(np, "reg", i * reg_cells,
  401. &cs);
  402. if (ret)
  403. return ret;
  404. if (cs > AT91_MATRIX_EBI_NUM_CS ||
  405. !(ebi->caps->available_cs & BIT(cs))) {
  406. dev_err(dev, "invalid reg property in %s\n",
  407. np->full_name);
  408. return -EINVAL;
  409. }
  410. ebid->configs[i].cs = cs;
  411. if (apply) {
  412. conf.cs = cs;
  413. ret = caps->apply_config(ebid, &conf);
  414. if (ret)
  415. return ret;
  416. }
  417. caps->get_config(ebid, &ebid->configs[i]);
  418. /*
  419. * Attach the EBI device to the generic SMC logic if at least
  420. * one "atmel,smc-" property is present.
  421. */
  422. if (ebi->ebi_csa && ret)
  423. regmap_field_update_bits(ebi->ebi_csa,
  424. BIT(cs), 0);
  425. }
  426. list_add_tail(&ebid->node, &ebi->devs);
  427. return 0;
  428. }
  429. static const struct reg_field at91sam9260_ebi_csa =
  430. REG_FIELD(AT91SAM9260_MATRIX_EBICSA, 0,
  431. AT91_MATRIX_EBI_NUM_CS - 1);
  432. static const struct at91_ebi_caps at91sam9260_ebi_caps = {
  433. .available_cs = 0xff,
  434. .ebi_csa = &at91sam9260_ebi_csa,
  435. .get_config = at91sam9_ebi_get_config,
  436. .xlate_config = at91sam9_ebi_xslate_config,
  437. .apply_config = at91sam9_ebi_apply_config,
  438. .init = at91sam9_ebi_init,
  439. };
  440. static const struct reg_field at91sam9261_ebi_csa =
  441. REG_FIELD(AT91SAM9261_MATRIX_EBICSA, 0,
  442. AT91_MATRIX_EBI_NUM_CS - 1);
  443. static const struct at91_ebi_caps at91sam9261_ebi_caps = {
  444. .available_cs = 0xff,
  445. .ebi_csa = &at91sam9261_ebi_csa,
  446. .get_config = at91sam9_ebi_get_config,
  447. .xlate_config = at91sam9_ebi_xslate_config,
  448. .apply_config = at91sam9_ebi_apply_config,
  449. .init = at91sam9_ebi_init,
  450. };
  451. static const struct reg_field at91sam9263_ebi0_csa =
  452. REG_FIELD(AT91SAM9263_MATRIX_EBI0CSA, 0,
  453. AT91_MATRIX_EBI_NUM_CS - 1);
  454. static const struct at91_ebi_caps at91sam9263_ebi0_caps = {
  455. .available_cs = 0x3f,
  456. .ebi_csa = &at91sam9263_ebi0_csa,
  457. .get_config = at91sam9_ebi_get_config,
  458. .xlate_config = at91sam9_ebi_xslate_config,
  459. .apply_config = at91sam9_ebi_apply_config,
  460. .init = at91sam9_ebi_init,
  461. };
  462. static const struct reg_field at91sam9263_ebi1_csa =
  463. REG_FIELD(AT91SAM9263_MATRIX_EBI1CSA, 0,
  464. AT91_MATRIX_EBI_NUM_CS - 1);
  465. static const struct at91_ebi_caps at91sam9263_ebi1_caps = {
  466. .available_cs = 0x7,
  467. .ebi_csa = &at91sam9263_ebi1_csa,
  468. .get_config = at91sam9_ebi_get_config,
  469. .xlate_config = at91sam9_ebi_xslate_config,
  470. .apply_config = at91sam9_ebi_apply_config,
  471. .init = at91sam9_ebi_init,
  472. };
  473. static const struct reg_field at91sam9rl_ebi_csa =
  474. REG_FIELD(AT91SAM9RL_MATRIX_EBICSA, 0,
  475. AT91_MATRIX_EBI_NUM_CS - 1);
  476. static const struct at91_ebi_caps at91sam9rl_ebi_caps = {
  477. .available_cs = 0x3f,
  478. .ebi_csa = &at91sam9rl_ebi_csa,
  479. .get_config = at91sam9_ebi_get_config,
  480. .xlate_config = at91sam9_ebi_xslate_config,
  481. .apply_config = at91sam9_ebi_apply_config,
  482. .init = at91sam9_ebi_init,
  483. };
  484. static const struct reg_field at91sam9g45_ebi_csa =
  485. REG_FIELD(AT91SAM9G45_MATRIX_EBICSA, 0,
  486. AT91_MATRIX_EBI_NUM_CS - 1);
  487. static const struct at91_ebi_caps at91sam9g45_ebi_caps = {
  488. .available_cs = 0x3f,
  489. .ebi_csa = &at91sam9g45_ebi_csa,
  490. .get_config = at91sam9_ebi_get_config,
  491. .xlate_config = at91sam9_ebi_xslate_config,
  492. .apply_config = at91sam9_ebi_apply_config,
  493. .init = at91sam9_ebi_init,
  494. };
  495. static const struct at91_ebi_caps at91sam9x5_ebi_caps = {
  496. .available_cs = 0x3f,
  497. .ebi_csa = &at91sam9263_ebi0_csa,
  498. .get_config = at91sam9_ebi_get_config,
  499. .xlate_config = at91sam9_ebi_xslate_config,
  500. .apply_config = at91sam9_ebi_apply_config,
  501. .init = at91sam9_ebi_init,
  502. };
  503. static const struct at91_ebi_caps sama5d3_ebi_caps = {
  504. .available_cs = 0xf,
  505. .get_config = at91sam9_ebi_get_config,
  506. .xlate_config = at91sam9_ebi_xslate_config,
  507. .apply_config = at91sam9_ebi_apply_config,
  508. .init = sama5d3_ebi_init,
  509. };
  510. static const struct of_device_id at91_ebi_id_table[] = {
  511. {
  512. .compatible = "atmel,at91sam9260-ebi",
  513. .data = &at91sam9260_ebi_caps,
  514. },
  515. {
  516. .compatible = "atmel,at91sam9261-ebi",
  517. .data = &at91sam9261_ebi_caps,
  518. },
  519. {
  520. .compatible = "atmel,at91sam9263-ebi0",
  521. .data = &at91sam9263_ebi0_caps,
  522. },
  523. {
  524. .compatible = "atmel,at91sam9263-ebi1",
  525. .data = &at91sam9263_ebi1_caps,
  526. },
  527. {
  528. .compatible = "atmel,at91sam9rl-ebi",
  529. .data = &at91sam9rl_ebi_caps,
  530. },
  531. {
  532. .compatible = "atmel,at91sam9g45-ebi",
  533. .data = &at91sam9g45_ebi_caps,
  534. },
  535. {
  536. .compatible = "atmel,at91sam9x5-ebi",
  537. .data = &at91sam9x5_ebi_caps,
  538. },
  539. {
  540. .compatible = "atmel,sama5d3-ebi",
  541. .data = &sama5d3_ebi_caps,
  542. },
  543. { /* sentinel */ }
  544. };
  545. static int at91_ebi_dev_disable(struct at91_ebi *ebi, struct device_node *np)
  546. {
  547. struct device *dev = ebi->dev;
  548. struct property *newprop;
  549. newprop = devm_kzalloc(dev, sizeof(*newprop), GFP_KERNEL);
  550. if (!newprop)
  551. return -ENOMEM;
  552. newprop->name = devm_kstrdup(dev, "status", GFP_KERNEL);
  553. if (!newprop->name)
  554. return -ENOMEM;
  555. newprop->value = devm_kstrdup(dev, "disabled", GFP_KERNEL);
  556. if (!newprop->name)
  557. return -ENOMEM;
  558. newprop->length = sizeof("disabled");
  559. return of_update_property(np, newprop);
  560. }
  561. static int at91_ebi_probe(struct platform_device *pdev)
  562. {
  563. struct device *dev = &pdev->dev;
  564. struct device_node *child, *np = dev->of_node;
  565. const struct of_device_id *match;
  566. struct at91_ebi *ebi;
  567. int ret, reg_cells;
  568. struct clk *clk;
  569. u32 val;
  570. match = of_match_device(at91_ebi_id_table, dev);
  571. if (!match || !match->data)
  572. return -EINVAL;
  573. ebi = devm_kzalloc(dev, sizeof(*ebi), GFP_KERNEL);
  574. if (!ebi)
  575. return -ENOMEM;
  576. INIT_LIST_HEAD(&ebi->devs);
  577. ebi->caps = match->data;
  578. ebi->dev = dev;
  579. clk = devm_clk_get(dev, NULL);
  580. if (IS_ERR(clk))
  581. return PTR_ERR(clk);
  582. ebi->clk = clk;
  583. ebi->smc = syscon_regmap_lookup_by_phandle(np, "atmel,smc");
  584. if (IS_ERR(ebi->smc))
  585. return PTR_ERR(ebi->smc);
  586. /*
  587. * The sama5d3 does not provide an EBICSA register and thus does need
  588. * to access the matrix registers.
  589. */
  590. if (ebi->caps->ebi_csa) {
  591. ebi->matrix =
  592. syscon_regmap_lookup_by_phandle(np, "atmel,matrix");
  593. if (IS_ERR(ebi->matrix))
  594. return PTR_ERR(ebi->matrix);
  595. ebi->ebi_csa = regmap_field_alloc(ebi->matrix,
  596. *ebi->caps->ebi_csa);
  597. if (IS_ERR(ebi->ebi_csa))
  598. return PTR_ERR(ebi->ebi_csa);
  599. }
  600. ret = ebi->caps->init(ebi);
  601. if (ret)
  602. return ret;
  603. ret = of_property_read_u32(np, "#address-cells", &val);
  604. if (ret) {
  605. dev_err(dev, "missing #address-cells property\n");
  606. return ret;
  607. }
  608. reg_cells = val;
  609. ret = of_property_read_u32(np, "#size-cells", &val);
  610. if (ret) {
  611. dev_err(dev, "missing #address-cells property\n");
  612. return ret;
  613. }
  614. reg_cells += val;
  615. for_each_available_child_of_node(np, child) {
  616. if (!of_find_property(child, "reg", NULL))
  617. continue;
  618. ret = at91_ebi_dev_setup(ebi, child, reg_cells);
  619. if (ret) {
  620. dev_err(dev, "failed to configure EBI bus for %s, disabling the device",
  621. child->full_name);
  622. ret = at91_ebi_dev_disable(ebi, child);
  623. if (ret)
  624. return ret;
  625. }
  626. }
  627. return of_platform_populate(np, NULL, NULL, dev);
  628. }
  629. static struct platform_driver at91_ebi_driver = {
  630. .driver = {
  631. .name = "atmel-ebi",
  632. .of_match_table = at91_ebi_id_table,
  633. },
  634. };
  635. builtin_platform_driver_probe(at91_ebi_driver, at91_ebi_probe);