pxa2x0.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /* $OpenBSD: pxa2x0.c,v 1.19 2014/10/18 12:21:56 miod Exp $ */
  2. /* $NetBSD: pxa2x0.c,v 1.5 2003/12/12 16:42:44 thorpej Exp $ */
  3. /*
  4. * Copyright (c) 2002 Genetec Corporation. All rights reserved.
  5. * Written by Hiroyuki Bessho for Genetec Corporation.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. All advertising materials mentioning features or use of this software
  16. * must display the following acknowledgement:
  17. * This product includes software developed for the NetBSD Project by
  18. * Genetec Corporation.
  19. * 4. The name of Genetec Corporation may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  25. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  26. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
  27. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. *
  36. * Autoconfiguration support for the Intel PXA2[15]0 application
  37. * processor. This code is derived from arm/sa11x0/sa11x0.c
  38. */
  39. /*-
  40. * Copyright (c) 2001, The NetBSD Foundation, Inc. All rights reserved.
  41. *
  42. * This code is derived from software contributed to The NetBSD Foundation
  43. * by IWAMOTO Toshihiro and Ichiro FUKUHARA.
  44. *
  45. * Redistribution and use in source and binary forms, with or without
  46. * modification, are permitted provided that the following conditions
  47. * are met:
  48. * 1. Redistributions of source code must retain the above copyright
  49. * notice, this list of conditions and the following disclaimer.
  50. * 2. Redistributions in binary form must reproduce the above copyright
  51. * notice, this list of conditions and the following disclaimer in the
  52. * documentation and/or other materials provided with the distribution.
  53. */
  54. /*-
  55. * Copyright (c) 1999
  56. * Shin Takemura and PocketBSD Project. All rights reserved.
  57. *
  58. * Redistribution and use in source and binary forms, with or without
  59. * modification, are permitted provided that the following conditions
  60. * are met:
  61. * 1. Redistributions of source code must retain the above copyright
  62. * notice, this list of conditions and the following disclaimer.
  63. * 2. Redistributions in binary form must reproduce the above copyright
  64. * notice, this list of conditions and the following disclaimer in the
  65. * documentation and/or other materials provided with the distribution.
  66. * 3. All advertising materials mentioning features or use of this software
  67. * must display the following acknowledgement:
  68. * This product includes software developed by the PocketBSD project
  69. * and its contributors.
  70. * 4. Neither the name of the project nor the names of its contributors
  71. * may be used to endorse or promote products derived from this software
  72. * without specific prior written permission.
  73. *
  74. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  75. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  76. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  77. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  78. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  79. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  80. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  81. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  82. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  83. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  84. * SUCH DAMAGE.
  85. *
  86. */
  87. #include "pxagpio.h"
  88. #include "pxadmac.h"
  89. #include <sys/param.h>
  90. #include <sys/systm.h>
  91. #include <sys/device.h>
  92. #include <sys/kernel.h>
  93. #include <sys/reboot.h>
  94. #include <sys/timetc.h>
  95. #include <machine/cpu.h>
  96. #include <machine/bus.h>
  97. #include <arm/cpufunc.h>
  98. #include <arm/mainbus/mainbus.h>
  99. #include <arm/xscale/pxa2x0reg.h>
  100. #include <arm/xscale/pxa2x0var.h>
  101. struct pxaip_softc {
  102. struct device sc_dev;
  103. bus_space_tag_t sc_bust;
  104. bus_dma_tag_t sc_dmat;
  105. bus_space_handle_t sc_bush_clk;
  106. bus_space_handle_t sc_bush_rtc;
  107. };
  108. /* prototypes */
  109. int pxaip_match(struct device *, void *, void *);
  110. void pxaip_attach(struct device *, struct device *, void *);
  111. int pxaip_search(struct device *, void *, void *);
  112. void pxaip_attach_critical(struct pxaip_softc *);
  113. int pxaip_print(void *, const char *);
  114. int pxaip_measure_cpuclock(struct pxaip_softc *);
  115. /* attach structures */
  116. #ifdef __NetBSD__
  117. CFATTACH_DECL(pxaip, sizeof(struct pxaip_softc),
  118. pxaip_match, pxaip_attach, NULL, NULL);
  119. #else
  120. struct cfattach pxaip_ca = {
  121. sizeof(struct pxaip_softc), pxaip_match, pxaip_attach
  122. };
  123. struct cfdriver pxaip_cd = {
  124. NULL, "pxaip", DV_DULL
  125. };
  126. #endif
  127. struct pxaip_softc *pxaip_sc;
  128. int
  129. pxaip_match(struct device *parent, void *match, void *aux)
  130. {
  131. return 1;
  132. }
  133. void
  134. pxaip_attach(struct device *parent, struct device *self, void *aux)
  135. {
  136. struct pxaip_softc *sc = (struct pxaip_softc *)self;
  137. #ifdef __APM__
  138. extern int freq;
  139. #endif
  140. int cpuclock;
  141. pxaip_sc = sc;
  142. sc->sc_bust = &pxa2x0_bs_tag;
  143. sc->sc_dmat = &pxa2x0_bus_dma_tag;
  144. if (bus_space_map(sc->sc_bust, PXA2X0_CLKMAN_BASE, PXA2X0_CLKMAN_SIZE,
  145. 0, &sc->sc_bush_clk))
  146. panic("pxaip_attach: failed to map CLKMAN");
  147. if (bus_space_map(sc->sc_bust, PXA2X0_RTC_BASE, PXA2X0_RTC_SIZE,
  148. 0, &sc->sc_bush_rtc))
  149. panic("pxaip_attach: failed to map RTC");
  150. /*
  151. * Calculate clock speed
  152. * This takes 2 secs at most.
  153. */
  154. cpuclock = pxaip_measure_cpuclock(sc) / 1000;
  155. if (cpuclock % 1000 > 500)
  156. cpuclock = cpuclock + 1000 - cpuclock % 1000;
  157. #ifdef __APM__
  158. freq = cpuclock / 1000;
  159. #endif
  160. printf(": CPU clock = %d.%03d MHz\n", cpuclock/1000, cpuclock%1000);
  161. /*
  162. * Attach critical devices
  163. */
  164. pxaip_attach_critical(sc);
  165. /*
  166. * Attach all other devices
  167. */
  168. config_search(pxaip_search, self, sc);
  169. }
  170. int
  171. pxaip_search(struct device *parent, void *c, void *aux)
  172. {
  173. struct pxaip_softc *sc = aux;
  174. struct pxaip_attach_args aa;
  175. struct cfdata *cf = c;
  176. aa.pxa_iot = sc->sc_bust;
  177. aa.pxa_dmat = sc->sc_dmat;
  178. #if 0
  179. aa.pxa_addr = cf->cf_addr;
  180. aa.pxa_size = cf->cf_size;
  181. aa.pxa_intr = cf->cf_intr;
  182. aa.pxa_index = cf->cf_index;
  183. #else
  184. aa.pxa_addr = (cf->cf_loc)[0];
  185. aa.pxa_size = (cf->cf_loc)[1];
  186. aa.pxa_intr = (cf->cf_loc)[2];
  187. aa.pxa_index = (cf->cf_loc)[3];
  188. #endif
  189. config_found(parent, &aa, pxaip_print);
  190. return 0;
  191. }
  192. void
  193. pxaip_attach_critical(struct pxaip_softc *sc)
  194. {
  195. struct pxaip_attach_args aa;
  196. aa.pxa_iot = sc->sc_bust;
  197. aa.pxa_dmat = sc->sc_dmat;
  198. aa.pxa_addr = PXA2X0_INTCTL_BASE;
  199. aa.pxa_size = PXA2X0_INTCTL_SIZE;
  200. aa.pxa_intr = -1;
  201. if (config_found(&sc->sc_dev, &aa, pxaip_print) == NULL)
  202. panic("pxaip_attach_critical: failed to attach INTC!");
  203. #if NPXAGPIO > 0
  204. aa.pxa_iot = sc->sc_bust;
  205. aa.pxa_dmat = sc->sc_dmat;
  206. aa.pxa_addr = PXA2X0_GPIO_BASE;
  207. aa.pxa_size = PXA2X0_GPIO_SIZE;
  208. aa.pxa_intr = -1;
  209. if (config_found(&sc->sc_dev, &aa, pxaip_print) == NULL)
  210. panic("pxaip_attach_critical: failed to attach GPIO!");
  211. #endif
  212. #if NPXADMAC > 0
  213. aa.pxa_iot = sc->sc_bust;
  214. aa.pxa_dmat = sc->sc_dmat;
  215. aa.pxa_addr = PXA2X0_DMAC_BASE;
  216. aa.pxa_size = PXA2X0_DMAC_SIZE;
  217. aa.pxa_intr = PXA2X0_INT_DMA;
  218. if (config_found(&sc->sc_dev, &aa, pxaip_print) == NULL)
  219. panic("pxaip_attach_critical: failed to attach DMAC!");
  220. #endif
  221. }
  222. int
  223. pxaip_print(void *aux, const char *name)
  224. {
  225. struct pxaip_attach_args *sa = (struct pxaip_attach_args*)aux;
  226. if (sa->pxa_addr != -1) {
  227. printf(" addr 0x%lx", sa->pxa_addr);
  228. if (sa->pxa_size > -1)
  229. printf("-0x%lx", sa->pxa_addr + sa->pxa_size-1);
  230. }
  231. if (sa->pxa_intr != -1)
  232. printf(" intr %d", sa->pxa_intr);
  233. return (UNCONF);
  234. }
  235. static inline uint32_t
  236. read_clock_counter(void)
  237. {
  238. uint32_t x;
  239. __asm volatile("mrc p14, 0, %0, c1, c1, 0" : "=r" (x));
  240. return x;
  241. }
  242. int
  243. pxaip_measure_cpuclock(struct pxaip_softc *sc)
  244. {
  245. uint32_t rtc0, rtc1, start, end;
  246. uint32_t pmcr_save;
  247. bus_space_handle_t ioh;
  248. int irq;
  249. ioh = sc->sc_bush_rtc;
  250. irq = disable_interrupts(I32_bit|F32_bit);
  251. __asm volatile( "mrc p14, 0, %0, c0, c1, 0" : "=r" (pmcr_save));
  252. /* Enable clock counter */
  253. __asm volatile( "mcr p14, 0, %0, c0, c1, 0" : : "r" (0x0001));
  254. rtc0 = bus_space_read_4(sc->sc_bust, ioh, RTC_RCNR);
  255. /* Wait for next second starts */
  256. while ((rtc1 = bus_space_read_4(sc->sc_bust, ioh, RTC_RCNR)) == rtc0)
  257. ;
  258. start = read_clock_counter();
  259. while(rtc1 == bus_space_read_4(sc->sc_bust, ioh, RTC_RCNR))
  260. ; /* Wait for 1sec */
  261. end = read_clock_counter();
  262. __asm volatile( "mcr p14, 0, %0, c0, c1, 0" : : "r" (pmcr_save));
  263. restore_interrupts(irq);
  264. return end - start;
  265. }
  266. void
  267. pxa2x0_turbo_mode(int f)
  268. {
  269. __asm volatile("mcr p14, 0, %0, c6, c0, 0" : : "r" (f));
  270. }
  271. void
  272. pxa2x0_probe_sdram(vaddr_t memctl_va, paddr_t *start, paddr_t *size)
  273. {
  274. u_int32_t mdcnfg, dwid, dcac, drac, dnb;
  275. int i;
  276. mdcnfg = *((volatile u_int32_t *)(memctl_va + MEMCTL_MDCNFG));
  277. /*
  278. * Scan all 4 SDRAM banks
  279. */
  280. for (i = 0; i < PXA2X0_SDRAM_BANKS; i++) {
  281. start[i] = 0;
  282. size[i] = 0;
  283. switch (i) {
  284. case 0:
  285. case 1:
  286. if ((i == 0 && (mdcnfg & MDCNFG_DE0) == 0) ||
  287. (i == 1 && (mdcnfg & MDCNFG_DE1) == 0))
  288. continue;
  289. dwid = mdcnfg >> MDCNFD_DWID01_SHIFT;
  290. dcac = mdcnfg >> MDCNFD_DCAC01_SHIFT;
  291. drac = mdcnfg >> MDCNFD_DRAC01_SHIFT;
  292. dnb = mdcnfg >> MDCNFD_DNB01_SHIFT;
  293. break;
  294. case 2:
  295. case 3:
  296. if ((i == 2 && (mdcnfg & MDCNFG_DE2) == 0) ||
  297. (i == 3 && (mdcnfg & MDCNFG_DE3) == 0))
  298. continue;
  299. dwid = mdcnfg >> MDCNFD_DWID23_SHIFT;
  300. dcac = mdcnfg >> MDCNFD_DCAC23_SHIFT;
  301. drac = mdcnfg >> MDCNFD_DRAC23_SHIFT;
  302. dnb = mdcnfg >> MDCNFD_DNB23_SHIFT;
  303. break;
  304. default:
  305. panic("pxa2x0_probe_sdram: impossible");
  306. }
  307. dwid = 2 << (1 - (dwid & MDCNFD_DWID_MASK)); /* 16/32 width */
  308. dcac = 1 << ((dcac & MDCNFD_DCAC_MASK) + 8); /* 8-11 columns */
  309. drac = 1 << ((drac & MDCNFD_DRAC_MASK) + 11); /* 11-13 rows */
  310. dnb = 2 << (dnb & MDCNFD_DNB_MASK); /* # of banks */
  311. size[i] = (paddr_t)(dwid * dcac * drac * dnb);
  312. start[i] = PXA2X0_SDRAM0_START + (i * PXA2X0_SDRAM_BANK_SIZE);
  313. }
  314. }
  315. void
  316. pxa2x0_clkman_config(u_int clk, int enable)
  317. {
  318. struct pxaip_softc *sc;
  319. u_int32_t rv;
  320. KDASSERT(pxaip_sc != NULL);
  321. sc = pxaip_sc;
  322. rv = bus_space_read_4(sc->sc_bust, sc->sc_bush_clk, CLKMAN_CKEN);
  323. rv &= ~clk;
  324. if (enable)
  325. rv |= clk;
  326. bus_space_write_4(sc->sc_bust, sc->sc_bush_clk, CLKMAN_CKEN, rv);
  327. }
  328. void
  329. pxa2x0_rtc_setalarm(u_int32_t secs)
  330. {
  331. struct pxaip_softc *sc;
  332. u_int32_t rv;
  333. int s;
  334. KDASSERT(pxaip_sc != NULL);
  335. sc = pxaip_sc;
  336. s = splhigh();
  337. bus_space_write_4(sc->sc_bust, sc->sc_bush_rtc, RTC_RTAR, secs);
  338. rv = bus_space_read_4(sc->sc_bust, sc->sc_bush_rtc, RTC_RTSR);
  339. if (secs == 0)
  340. bus_space_write_4(sc->sc_bust, sc->sc_bush_rtc, RTC_RTSR,
  341. (rv | RTSR_AL) & ~RTSR_ALE);
  342. else
  343. bus_space_write_4(sc->sc_bust, sc->sc_bush_rtc, RTC_RTSR,
  344. (rv | RTSR_AL | RTSR_ALE));
  345. splx(s);
  346. }
  347. u_int32_t
  348. pxa2x0_rtc_getalarm(void)
  349. {
  350. struct pxaip_softc *sc;
  351. KDASSERT(pxaip_sc != NULL);
  352. sc = pxaip_sc;
  353. return (bus_space_read_4(sc->sc_bust, sc->sc_bush_rtc, RTC_RTAR));
  354. }
  355. u_int32_t
  356. pxa2x0_rtc_getsecs(void)
  357. {
  358. struct pxaip_softc *sc;
  359. KDASSERT(pxaip_sc != NULL);
  360. sc = pxaip_sc;
  361. return (bus_space_read_4(sc->sc_bust, sc->sc_bush_rtc, RTC_RCNR));
  362. }
  363. void
  364. resettodr(void)
  365. {
  366. struct pxaip_softc *sc = pxaip_sc;
  367. struct timeval tv;
  368. microtime(&tv);
  369. bus_space_write_4(sc->sc_bust, sc->sc_bush_rtc, RTC_RCNR,
  370. (u_int32_t)tv.tv_sec);
  371. }
  372. void
  373. inittodr(time_t base)
  374. {
  375. struct pxaip_softc *sc = pxaip_sc;
  376. struct timespec ts;
  377. u_int32_t rcnr;
  378. /* XXX decide if RCNR can be valid, based on the last reset
  379. * XXX reason, i.e. RCSR. */
  380. rcnr = bus_space_read_4(sc->sc_bust, sc->sc_bush_rtc, RTC_RCNR);
  381. /* XXX check how much RCNR differs from the filesystem date. */
  382. if (rcnr > base)
  383. ts.tv_sec = rcnr;
  384. else {
  385. printf("WARNING: using filesystem date -- CHECK AND RESET THE DATE!\n");
  386. ts.tv_sec = base;
  387. }
  388. ts.tv_nsec = 0;
  389. tc_setclock(&ts);
  390. }