core_marvel.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /*
  2. * linux/arch/alpha/kernel/core_marvel.c
  3. *
  4. * Code common to all Marvel based systems.
  5. */
  6. #define __EXTERN_INLINE inline
  7. #include <asm/io.h>
  8. #include <asm/core_marvel.h>
  9. #undef __EXTERN_INLINE
  10. #include <linux/types.h>
  11. #include <linux/pci.h>
  12. #include <linux/sched.h>
  13. #include <linux/init.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/mc146818rtc.h>
  16. #include <linux/rtc.h>
  17. #include <linux/module.h>
  18. #include <linux/bootmem.h>
  19. #include <asm/ptrace.h>
  20. #include <asm/smp.h>
  21. #include <asm/gct.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/vga.h>
  25. #include "proto.h"
  26. #include "pci_impl.h"
  27. /*
  28. * Debug helpers
  29. */
  30. #define DEBUG_CONFIG 0
  31. #if DEBUG_CONFIG
  32. # define DBG_CFG(args) printk args
  33. #else
  34. # define DBG_CFG(args)
  35. #endif
  36. /*
  37. * Private data
  38. */
  39. static struct io7 *io7_head = NULL;
  40. /*
  41. * Helper functions
  42. */
  43. static unsigned long __attribute__ ((unused))
  44. read_ev7_csr(int pe, unsigned long offset)
  45. {
  46. ev7_csr *ev7csr = EV7_CSR_KERN(pe, offset);
  47. unsigned long q;
  48. mb();
  49. q = ev7csr->csr;
  50. mb();
  51. return q;
  52. }
  53. static void __attribute__ ((unused))
  54. write_ev7_csr(int pe, unsigned long offset, unsigned long q)
  55. {
  56. ev7_csr *ev7csr = EV7_CSR_KERN(pe, offset);
  57. mb();
  58. ev7csr->csr = q;
  59. mb();
  60. }
  61. static char * __init
  62. mk_resource_name(int pe, int port, char *str)
  63. {
  64. char tmp[80];
  65. char *name;
  66. sprintf(tmp, "PCI %s PE %d PORT %d", str, pe, port);
  67. name = alloc_bootmem(strlen(tmp) + 1);
  68. strcpy(name, tmp);
  69. return name;
  70. }
  71. inline struct io7 *
  72. marvel_next_io7(struct io7 *prev)
  73. {
  74. return (prev ? prev->next : io7_head);
  75. }
  76. struct io7 *
  77. marvel_find_io7(int pe)
  78. {
  79. struct io7 *io7;
  80. for (io7 = io7_head; io7 && io7->pe != pe; io7 = io7->next)
  81. continue;
  82. return io7;
  83. }
  84. static struct io7 * __init
  85. alloc_io7(unsigned int pe)
  86. {
  87. struct io7 *io7;
  88. struct io7 *insp;
  89. int h;
  90. if (marvel_find_io7(pe)) {
  91. printk(KERN_WARNING "IO7 at PE %d already allocated!\n", pe);
  92. return NULL;
  93. }
  94. io7 = alloc_bootmem(sizeof(*io7));
  95. io7->pe = pe;
  96. spin_lock_init(&io7->irq_lock);
  97. for (h = 0; h < 4; h++) {
  98. io7->ports[h].io7 = io7;
  99. io7->ports[h].port = h;
  100. io7->ports[h].enabled = 0; /* default to disabled */
  101. }
  102. /*
  103. * Insert in pe sorted order.
  104. */
  105. if (NULL == io7_head) /* empty list */
  106. io7_head = io7;
  107. else if (io7_head->pe > io7->pe) { /* insert at head */
  108. io7->next = io7_head;
  109. io7_head = io7;
  110. } else { /* insert at position */
  111. for (insp = io7_head; insp; insp = insp->next) {
  112. if (insp->pe == io7->pe) {
  113. printk(KERN_ERR "Too many IO7s at PE %d\n",
  114. io7->pe);
  115. return NULL;
  116. }
  117. if (NULL == insp->next ||
  118. insp->next->pe > io7->pe) { /* insert here */
  119. io7->next = insp->next;
  120. insp->next = io7;
  121. break;
  122. }
  123. }
  124. if (NULL == insp) { /* couldn't insert ?!? */
  125. printk(KERN_WARNING "Failed to insert IO7 at PE %d "
  126. " - adding at head of list\n", io7->pe);
  127. io7->next = io7_head;
  128. io7_head = io7;
  129. }
  130. }
  131. return io7;
  132. }
  133. void
  134. io7_clear_errors(struct io7 *io7)
  135. {
  136. io7_port7_csrs *p7csrs;
  137. io7_ioport_csrs *csrs;
  138. int port;
  139. /*
  140. * First the IO ports.
  141. */
  142. for (port = 0; port < 4; port++) {
  143. csrs = IO7_CSRS_KERN(io7->pe, port);
  144. csrs->POx_ERR_SUM.csr = -1UL;
  145. csrs->POx_TLB_ERR.csr = -1UL;
  146. csrs->POx_SPL_COMPLT.csr = -1UL;
  147. csrs->POx_TRANS_SUM.csr = -1UL;
  148. }
  149. /*
  150. * Then the common ones.
  151. */
  152. p7csrs = IO7_PORT7_CSRS_KERN(io7->pe);
  153. p7csrs->PO7_ERROR_SUM.csr = -1UL;
  154. p7csrs->PO7_UNCRR_SYM.csr = -1UL;
  155. p7csrs->PO7_CRRCT_SYM.csr = -1UL;
  156. }
  157. /*
  158. * IO7 PCI, PCI/X, AGP configuration.
  159. */
  160. static void __init
  161. io7_init_hose(struct io7 *io7, int port)
  162. {
  163. static int hose_index = 0;
  164. struct pci_controller *hose = alloc_pci_controller();
  165. struct io7_port *io7_port = &io7->ports[port];
  166. io7_ioport_csrs *csrs = IO7_CSRS_KERN(io7->pe, port);
  167. int i;
  168. hose->index = hose_index++; /* arbitrary */
  169. /*
  170. * We don't have an isa or legacy hose, but glibc expects to be
  171. * able to use the bus == 0 / dev == 0 form of the iobase syscall
  172. * to determine information about the i/o system. Since XFree86
  173. * relies on glibc's determination to tell whether or not to use
  174. * sparse access, we need to point the pci_isa_hose at a real hose
  175. * so at least that determination is correct.
  176. */
  177. if (hose->index == 0)
  178. pci_isa_hose = hose;
  179. io7_port->csrs = csrs;
  180. io7_port->hose = hose;
  181. hose->sysdata = io7_port;
  182. hose->io_space = alloc_resource();
  183. hose->mem_space = alloc_resource();
  184. /*
  185. * Base addresses for userland consumption. Since these are going
  186. * to be mapped, they are pure physical addresses.
  187. */
  188. hose->sparse_mem_base = hose->sparse_io_base = 0;
  189. hose->dense_mem_base = IO7_MEM_PHYS(io7->pe, port);
  190. hose->dense_io_base = IO7_IO_PHYS(io7->pe, port);
  191. /*
  192. * Base addresses and resource ranges for kernel consumption.
  193. */
  194. hose->config_space_base = (unsigned long)IO7_CONF_KERN(io7->pe, port);
  195. hose->io_space->start = (unsigned long)IO7_IO_KERN(io7->pe, port);
  196. hose->io_space->end = hose->io_space->start + IO7_IO_SPACE - 1;
  197. hose->io_space->name = mk_resource_name(io7->pe, port, "IO");
  198. hose->io_space->flags = IORESOURCE_IO;
  199. hose->mem_space->start = (unsigned long)IO7_MEM_KERN(io7->pe, port);
  200. hose->mem_space->end = hose->mem_space->start + IO7_MEM_SPACE - 1;
  201. hose->mem_space->name = mk_resource_name(io7->pe, port, "MEM");
  202. hose->mem_space->flags = IORESOURCE_MEM;
  203. if (request_resource(&ioport_resource, hose->io_space) < 0)
  204. printk(KERN_ERR "Failed to request IO on hose %d\n",
  205. hose->index);
  206. if (request_resource(&iomem_resource, hose->mem_space) < 0)
  207. printk(KERN_ERR "Failed to request MEM on hose %d\n",
  208. hose->index);
  209. /*
  210. * Save the existing DMA window settings for later restoration.
  211. */
  212. for (i = 0; i < 4; i++) {
  213. io7_port->saved_wbase[i] = csrs->POx_WBASE[i].csr;
  214. io7_port->saved_wmask[i] = csrs->POx_WMASK[i].csr;
  215. io7_port->saved_tbase[i] = csrs->POx_TBASE[i].csr;
  216. }
  217. /*
  218. * Set up the PCI to main memory translation windows.
  219. *
  220. * Window 0 is scatter-gather 8MB at 8MB
  221. * Window 1 is direct access 1GB at 2GB
  222. * Window 2 is scatter-gather (up-to) 1GB at 3GB
  223. * Window 3 is disabled
  224. */
  225. /*
  226. * TBIA before modifying windows.
  227. */
  228. marvel_pci_tbi(hose, 0, -1);
  229. /*
  230. * Set up window 0 for scatter-gather 8MB at 8MB.
  231. */
  232. hose->sg_isa = iommu_arena_new_node(marvel_cpuid_to_nid(io7->pe),
  233. hose, 0x00800000, 0x00800000, 0);
  234. hose->sg_isa->align_entry = 8; /* cache line boundary */
  235. csrs->POx_WBASE[0].csr =
  236. hose->sg_isa->dma_base | wbase_m_ena | wbase_m_sg;
  237. csrs->POx_WMASK[0].csr = (hose->sg_isa->size - 1) & wbase_m_addr;
  238. csrs->POx_TBASE[0].csr = virt_to_phys(hose->sg_isa->ptes);
  239. /*
  240. * Set up window 1 for direct-mapped 1GB at 2GB.
  241. */
  242. csrs->POx_WBASE[1].csr = __direct_map_base | wbase_m_ena;
  243. csrs->POx_WMASK[1].csr = (__direct_map_size - 1) & wbase_m_addr;
  244. csrs->POx_TBASE[1].csr = 0;
  245. /*
  246. * Set up window 2 for scatter-gather (up-to) 1GB at 3GB.
  247. */
  248. hose->sg_pci = iommu_arena_new_node(marvel_cpuid_to_nid(io7->pe),
  249. hose, 0xc0000000, 0x40000000, 0);
  250. hose->sg_pci->align_entry = 8; /* cache line boundary */
  251. csrs->POx_WBASE[2].csr =
  252. hose->sg_pci->dma_base | wbase_m_ena | wbase_m_sg;
  253. csrs->POx_WMASK[2].csr = (hose->sg_pci->size - 1) & wbase_m_addr;
  254. csrs->POx_TBASE[2].csr = virt_to_phys(hose->sg_pci->ptes);
  255. /*
  256. * Disable window 3.
  257. */
  258. csrs->POx_WBASE[3].csr = 0;
  259. /*
  260. * Make sure that the AGP Monster Window is disabled.
  261. */
  262. csrs->POx_CTRL.csr &= ~(1UL << 61);
  263. #if 1
  264. printk("FIXME: disabling master aborts\n");
  265. csrs->POx_MSK_HEI.csr &= ~(3UL << 14);
  266. #endif
  267. /*
  268. * TBIA after modifying windows.
  269. */
  270. marvel_pci_tbi(hose, 0, -1);
  271. }
  272. static void __init
  273. marvel_init_io7(struct io7 *io7)
  274. {
  275. int i;
  276. printk("Initializing IO7 at PID %d\n", io7->pe);
  277. /*
  278. * Get the Port 7 CSR pointer.
  279. */
  280. io7->csrs = IO7_PORT7_CSRS_KERN(io7->pe);
  281. /*
  282. * Init this IO7's hoses.
  283. */
  284. for (i = 0; i < IO7_NUM_PORTS; i++) {
  285. io7_ioport_csrs *csrs = IO7_CSRS_KERN(io7->pe, i);
  286. if (csrs->POx_CACHE_CTL.csr == 8) {
  287. io7->ports[i].enabled = 1;
  288. io7_init_hose(io7, i);
  289. }
  290. }
  291. }
  292. void
  293. marvel_io7_present(gct6_node *node)
  294. {
  295. int pe;
  296. if (node->type != GCT_TYPE_HOSE ||
  297. node->subtype != GCT_SUBTYPE_IO_PORT_MODULE)
  298. return;
  299. pe = (node->id >> 8) & 0xff;
  300. printk("Found an IO7 at PID %d\n", pe);
  301. alloc_io7(pe);
  302. }
  303. static void __init
  304. marvel_find_console_vga_hose(void)
  305. {
  306. u64 *pu64 = (u64 *)((u64)hwrpb + hwrpb->ctbt_offset);
  307. if (pu64[7] == 3) { /* TERM_TYPE == graphics */
  308. struct pci_controller *hose = NULL;
  309. int h = (pu64[30] >> 24) & 0xff; /* TERM_OUT_LOC, hose # */
  310. struct io7 *io7;
  311. int pid, port;
  312. /* FIXME - encoding is going to have to change for Marvel
  313. * since hose will be able to overflow a byte...
  314. * need to fix this decode when the console
  315. * changes its encoding
  316. */
  317. printk("console graphics is on hose %d (console)\n", h);
  318. /*
  319. * The console's hose numbering is:
  320. *
  321. * hose<n:2>: PID
  322. * hose<1:0>: PORT
  323. *
  324. * We need to find the hose at that pid and port
  325. */
  326. pid = h >> 2;
  327. port = h & 3;
  328. if ((io7 = marvel_find_io7(pid)))
  329. hose = io7->ports[port].hose;
  330. if (hose) {
  331. printk("Console graphics on hose %d\n", hose->index);
  332. pci_vga_hose = hose;
  333. }
  334. }
  335. }
  336. gct6_search_struct gct_wanted_node_list[] = {
  337. { GCT_TYPE_HOSE, GCT_SUBTYPE_IO_PORT_MODULE, marvel_io7_present },
  338. { 0, 0, NULL }
  339. };
  340. /*
  341. * In case the GCT is not complete, let the user specify PIDs with IO7s
  342. * at boot time. Syntax is 'io7=a,b,c,...,n' where a-n are the PIDs (decimal)
  343. * where IO7s are connected
  344. */
  345. static int __init
  346. marvel_specify_io7(char *str)
  347. {
  348. unsigned long pid;
  349. struct io7 *io7;
  350. char *pchar;
  351. do {
  352. pid = simple_strtoul(str, &pchar, 0);
  353. if (pchar != str) {
  354. printk("User-specified IO7 at PID %lu\n", pid);
  355. io7 = alloc_io7(pid);
  356. if (io7) marvel_init_io7(io7);
  357. }
  358. if (pchar == str) pchar++;
  359. str = pchar;
  360. } while(*str);
  361. return 1;
  362. }
  363. __setup("io7=", marvel_specify_io7);
  364. void __init
  365. marvel_init_arch(void)
  366. {
  367. struct io7 *io7;
  368. /* With multiple PCI busses, we play with I/O as physical addrs. */
  369. ioport_resource.end = ~0UL;
  370. /* PCI DMA Direct Mapping is 1GB at 2GB. */
  371. __direct_map_base = 0x80000000;
  372. __direct_map_size = 0x40000000;
  373. /* Parse the config tree. */
  374. gct6_find_nodes(GCT_NODE_PTR(0), gct_wanted_node_list);
  375. /* Init the io7s. */
  376. for (io7 = NULL; NULL != (io7 = marvel_next_io7(io7)); )
  377. marvel_init_io7(io7);
  378. /* Check for graphic console location (if any). */
  379. marvel_find_console_vga_hose();
  380. }
  381. void
  382. marvel_kill_arch(int mode)
  383. {
  384. }
  385. /*
  386. * PCI Configuration Space access functions
  387. *
  388. * Configuration space addresses have the following format:
  389. *
  390. * |2 2 2 2|1 1 1 1|1 1 1 1|1 1
  391. * |3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0
  392. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  393. * |B|B|B|B|B|B|B|B|D|D|D|D|D|F|F|F|R|R|R|R|R|R|R|R|
  394. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  395. *
  396. * n:24 reserved for hose base
  397. * 23:16 bus number (8 bits = 128 possible buses)
  398. * 15:11 Device number (5 bits)
  399. * 10:8 function number
  400. * 7:2 register number
  401. *
  402. * Notes:
  403. * IO7 determines whether to use a type 0 or type 1 config cycle
  404. * based on the bus number. Therefore the bus number must be set
  405. * to 0 for the root bus on any hose.
  406. *
  407. * The function number selects which function of a multi-function device
  408. * (e.g., SCSI and Ethernet).
  409. *
  410. */
  411. static inline unsigned long
  412. build_conf_addr(struct pci_controller *hose, u8 bus,
  413. unsigned int devfn, int where)
  414. {
  415. return (hose->config_space_base | (bus << 16) | (devfn << 8) | where);
  416. }
  417. static unsigned long
  418. mk_conf_addr(struct pci_bus *pbus, unsigned int devfn, int where)
  419. {
  420. struct pci_controller *hose = pbus->sysdata;
  421. struct io7_port *io7_port;
  422. unsigned long addr = 0;
  423. u8 bus = pbus->number;
  424. if (!hose)
  425. return addr;
  426. /* Check for enabled. */
  427. io7_port = hose->sysdata;
  428. if (!io7_port->enabled)
  429. return addr;
  430. if (!pbus->parent) { /* No parent means peer PCI bus. */
  431. /* Don't support idsel > 20 on primary bus. */
  432. if (devfn >= PCI_DEVFN(21, 0))
  433. return addr;
  434. bus = 0;
  435. }
  436. addr = build_conf_addr(hose, bus, devfn, where);
  437. DBG_CFG(("mk_conf_addr: returning pci_addr 0x%lx\n", addr));
  438. return addr;
  439. }
  440. static int
  441. marvel_read_config(struct pci_bus *bus, unsigned int devfn, int where,
  442. int size, u32 *value)
  443. {
  444. unsigned long addr;
  445. if (0 == (addr = mk_conf_addr(bus, devfn, where)))
  446. return PCIBIOS_DEVICE_NOT_FOUND;
  447. switch(size) {
  448. case 1:
  449. *value = __kernel_ldbu(*(vucp)addr);
  450. break;
  451. case 2:
  452. *value = __kernel_ldwu(*(vusp)addr);
  453. break;
  454. case 4:
  455. *value = *(vuip)addr;
  456. break;
  457. default:
  458. return PCIBIOS_FUNC_NOT_SUPPORTED;
  459. }
  460. return PCIBIOS_SUCCESSFUL;
  461. }
  462. static int
  463. marvel_write_config(struct pci_bus *bus, unsigned int devfn, int where,
  464. int size, u32 value)
  465. {
  466. unsigned long addr;
  467. if (0 == (addr = mk_conf_addr(bus, devfn, where)))
  468. return PCIBIOS_DEVICE_NOT_FOUND;
  469. switch (size) {
  470. case 1:
  471. __kernel_stb(value, *(vucp)addr);
  472. mb();
  473. __kernel_ldbu(*(vucp)addr);
  474. break;
  475. case 2:
  476. __kernel_stw(value, *(vusp)addr);
  477. mb();
  478. __kernel_ldwu(*(vusp)addr);
  479. break;
  480. case 4:
  481. *(vuip)addr = value;
  482. mb();
  483. *(vuip)addr;
  484. break;
  485. default:
  486. return PCIBIOS_FUNC_NOT_SUPPORTED;
  487. }
  488. return PCIBIOS_SUCCESSFUL;
  489. }
  490. struct pci_ops marvel_pci_ops =
  491. {
  492. .read = marvel_read_config,
  493. .write = marvel_write_config,
  494. };
  495. /*
  496. * Other PCI helper functions.
  497. */
  498. void
  499. marvel_pci_tbi(struct pci_controller *hose, dma_addr_t start, dma_addr_t end)
  500. {
  501. io7_ioport_csrs *csrs = ((struct io7_port *)hose->sysdata)->csrs;
  502. wmb();
  503. csrs->POx_SG_TBIA.csr = 0;
  504. mb();
  505. csrs->POx_SG_TBIA.csr;
  506. }
  507. /*
  508. * RTC Support
  509. */
  510. struct marvel_rtc_access_info {
  511. unsigned long function;
  512. unsigned long index;
  513. unsigned long data;
  514. };
  515. static void
  516. __marvel_access_rtc(void *info)
  517. {
  518. struct marvel_rtc_access_info *rtc_access = info;
  519. register unsigned long __r0 __asm__("$0");
  520. register unsigned long __r16 __asm__("$16") = rtc_access->function;
  521. register unsigned long __r17 __asm__("$17") = rtc_access->index;
  522. register unsigned long __r18 __asm__("$18") = rtc_access->data;
  523. __asm__ __volatile__(
  524. "call_pal %4 # cserve rtc"
  525. : "=r"(__r16), "=r"(__r17), "=r"(__r18), "=r"(__r0)
  526. : "i"(PAL_cserve), "0"(__r16), "1"(__r17), "2"(__r18)
  527. : "$1", "$22", "$23", "$24", "$25");
  528. rtc_access->data = __r0;
  529. }
  530. static u8
  531. __marvel_rtc_io(u8 b, unsigned long addr, int write)
  532. {
  533. static u8 index = 0;
  534. struct marvel_rtc_access_info rtc_access;
  535. u8 ret = 0;
  536. switch(addr) {
  537. case 0x70: /* RTC_PORT(0) */
  538. if (write) index = b;
  539. ret = index;
  540. break;
  541. case 0x71: /* RTC_PORT(1) */
  542. rtc_access.index = index;
  543. rtc_access.data = bcd2bin(b);
  544. rtc_access.function = 0x48 + !write; /* GET/PUT_TOY */
  545. __marvel_access_rtc(&rtc_access);
  546. ret = bin2bcd(rtc_access.data);
  547. break;
  548. default:
  549. printk(KERN_WARNING "Illegal RTC port %lx\n", addr);
  550. break;
  551. }
  552. return ret;
  553. }
  554. /*
  555. * IO map support.
  556. */
  557. void __iomem *
  558. marvel_ioremap(unsigned long addr, unsigned long size)
  559. {
  560. struct pci_controller *hose;
  561. unsigned long baddr, last;
  562. struct vm_struct *area;
  563. unsigned long vaddr;
  564. unsigned long *ptes;
  565. unsigned long pfn;
  566. /*
  567. * Adjust the address.
  568. */
  569. FIXUP_MEMADDR_VGA(addr);
  570. /*
  571. * Find the hose.
  572. */
  573. for (hose = hose_head; hose; hose = hose->next) {
  574. if ((addr >> 32) == (hose->mem_space->start >> 32))
  575. break;
  576. }
  577. if (!hose)
  578. return NULL;
  579. /*
  580. * We have the hose - calculate the bus limits.
  581. */
  582. baddr = addr - hose->mem_space->start;
  583. last = baddr + size - 1;
  584. /*
  585. * Is it direct-mapped?
  586. */
  587. if ((baddr >= __direct_map_base) &&
  588. ((baddr + size - 1) < __direct_map_base + __direct_map_size)) {
  589. addr = IDENT_ADDR | (baddr - __direct_map_base);
  590. return (void __iomem *) addr;
  591. }
  592. /*
  593. * Check the scatter-gather arena.
  594. */
  595. if (hose->sg_pci &&
  596. baddr >= (unsigned long)hose->sg_pci->dma_base &&
  597. last < (unsigned long)hose->sg_pci->dma_base + hose->sg_pci->size) {
  598. /*
  599. * Adjust the limits (mappings must be page aligned)
  600. */
  601. baddr -= hose->sg_pci->dma_base;
  602. last -= hose->sg_pci->dma_base;
  603. baddr &= PAGE_MASK;
  604. size = PAGE_ALIGN(last) - baddr;
  605. /*
  606. * Map it.
  607. */
  608. area = get_vm_area(size, VM_IOREMAP);
  609. if (!area)
  610. return NULL;
  611. ptes = hose->sg_pci->ptes;
  612. for (vaddr = (unsigned long)area->addr;
  613. baddr <= last;
  614. baddr += PAGE_SIZE, vaddr += PAGE_SIZE) {
  615. pfn = ptes[baddr >> PAGE_SHIFT];
  616. if (!(pfn & 1)) {
  617. printk("ioremap failed... pte not valid...\n");
  618. vfree(area->addr);
  619. return NULL;
  620. }
  621. pfn >>= 1; /* make it a true pfn */
  622. if (__alpha_remap_area_pages(vaddr,
  623. pfn << PAGE_SHIFT,
  624. PAGE_SIZE, 0)) {
  625. printk("FAILED to map...\n");
  626. vfree(area->addr);
  627. return NULL;
  628. }
  629. }
  630. flush_tlb_all();
  631. vaddr = (unsigned long)area->addr + (addr & ~PAGE_MASK);
  632. return (void __iomem *) vaddr;
  633. }
  634. /* Assume it was already a reasonable address */
  635. vaddr = baddr + hose->mem_space->start;
  636. return (void __iomem *) vaddr;
  637. }
  638. void
  639. marvel_iounmap(volatile void __iomem *xaddr)
  640. {
  641. unsigned long addr = (unsigned long) xaddr;
  642. if (addr >= VMALLOC_START)
  643. vfree((void *)(PAGE_MASK & addr));
  644. }
  645. int
  646. marvel_is_mmio(const volatile void __iomem *xaddr)
  647. {
  648. unsigned long addr = (unsigned long) xaddr;
  649. if (addr >= VMALLOC_START)
  650. return 1;
  651. else
  652. return (addr & 0xFF000000UL) == 0;
  653. }
  654. #define __marvel_is_port_kbd(a) (((a) == 0x60) || ((a) == 0x64))
  655. #define __marvel_is_port_rtc(a) (((a) == 0x70) || ((a) == 0x71))
  656. void __iomem *marvel_ioportmap (unsigned long addr)
  657. {
  658. FIXUP_IOADDR_VGA(addr);
  659. return (void __iomem *)addr;
  660. }
  661. unsigned int
  662. marvel_ioread8(void __iomem *xaddr)
  663. {
  664. unsigned long addr = (unsigned long) xaddr;
  665. if (__marvel_is_port_kbd(addr))
  666. return 0;
  667. else if (__marvel_is_port_rtc(addr))
  668. return __marvel_rtc_io(0, addr, 0);
  669. else if (marvel_is_ioaddr(addr))
  670. return __kernel_ldbu(*(vucp)addr);
  671. else
  672. /* this should catch other legacy addresses
  673. that would normally fail on MARVEL,
  674. because there really is nothing there...
  675. */
  676. return ~0;
  677. }
  678. void
  679. marvel_iowrite8(u8 b, void __iomem *xaddr)
  680. {
  681. unsigned long addr = (unsigned long) xaddr;
  682. if (__marvel_is_port_kbd(addr))
  683. return;
  684. else if (__marvel_is_port_rtc(addr))
  685. __marvel_rtc_io(b, addr, 1);
  686. else if (marvel_is_ioaddr(addr))
  687. __kernel_stb(b, *(vucp)addr);
  688. }
  689. #ifndef CONFIG_ALPHA_GENERIC
  690. EXPORT_SYMBOL(marvel_ioremap);
  691. EXPORT_SYMBOL(marvel_iounmap);
  692. EXPORT_SYMBOL(marvel_is_mmio);
  693. EXPORT_SYMBOL(marvel_ioportmap);
  694. EXPORT_SYMBOL(marvel_ioread8);
  695. EXPORT_SYMBOL(marvel_iowrite8);
  696. #endif
  697. /*
  698. * NUMA Support
  699. */
  700. /**********
  701. * FIXME - for now each cpu is a node by itself
  702. * -- no real support for striped mode
  703. **********
  704. */
  705. int
  706. marvel_pa_to_nid(unsigned long pa)
  707. {
  708. int cpuid;
  709. if ((pa >> 43) & 1) /* I/O */
  710. cpuid = (~(pa >> 35) & 0xff);
  711. else /* mem */
  712. cpuid = ((pa >> 34) & 0x3) | ((pa >> (37 - 2)) & (0x1f << 2));
  713. return marvel_cpuid_to_nid(cpuid);
  714. }
  715. int
  716. marvel_cpuid_to_nid(int cpuid)
  717. {
  718. return cpuid;
  719. }
  720. unsigned long
  721. marvel_node_mem_start(int nid)
  722. {
  723. unsigned long pa;
  724. pa = (nid & 0x3) | ((nid & (0x1f << 2)) << 1);
  725. pa <<= 34;
  726. return pa;
  727. }
  728. unsigned long
  729. marvel_node_mem_size(int nid)
  730. {
  731. return 16UL * 1024 * 1024 * 1024; /* 16GB */
  732. }
  733. /*
  734. * AGP GART Support.
  735. */
  736. #include <linux/agp_backend.h>
  737. #include <asm/agp_backend.h>
  738. #include <linux/slab.h>
  739. #include <linux/delay.h>
  740. struct marvel_agp_aperture {
  741. struct pci_iommu_arena *arena;
  742. long pg_start;
  743. long pg_count;
  744. };
  745. static int
  746. marvel_agp_setup(alpha_agp_info *agp)
  747. {
  748. struct marvel_agp_aperture *aper;
  749. if (!alpha_agpgart_size)
  750. return -ENOMEM;
  751. aper = kmalloc(sizeof(*aper), GFP_KERNEL);
  752. if (aper == NULL) return -ENOMEM;
  753. aper->arena = agp->hose->sg_pci;
  754. aper->pg_count = alpha_agpgart_size / PAGE_SIZE;
  755. aper->pg_start = iommu_reserve(aper->arena, aper->pg_count,
  756. aper->pg_count - 1);
  757. if (aper->pg_start < 0) {
  758. printk(KERN_ERR "Failed to reserve AGP memory\n");
  759. kfree(aper);
  760. return -ENOMEM;
  761. }
  762. agp->aperture.bus_base =
  763. aper->arena->dma_base + aper->pg_start * PAGE_SIZE;
  764. agp->aperture.size = aper->pg_count * PAGE_SIZE;
  765. agp->aperture.sysdata = aper;
  766. return 0;
  767. }
  768. static void
  769. marvel_agp_cleanup(alpha_agp_info *agp)
  770. {
  771. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  772. int status;
  773. status = iommu_release(aper->arena, aper->pg_start, aper->pg_count);
  774. if (status == -EBUSY) {
  775. printk(KERN_WARNING
  776. "Attempted to release bound AGP memory - unbinding\n");
  777. iommu_unbind(aper->arena, aper->pg_start, aper->pg_count);
  778. status = iommu_release(aper->arena, aper->pg_start,
  779. aper->pg_count);
  780. }
  781. if (status < 0)
  782. printk(KERN_ERR "Failed to release AGP memory\n");
  783. kfree(aper);
  784. kfree(agp);
  785. }
  786. static int
  787. marvel_agp_configure(alpha_agp_info *agp)
  788. {
  789. io7_ioport_csrs *csrs = ((struct io7_port *)agp->hose->sysdata)->csrs;
  790. struct io7 *io7 = ((struct io7_port *)agp->hose->sysdata)->io7;
  791. unsigned int new_rate = 0;
  792. unsigned long agp_pll;
  793. /*
  794. * Check the requested mode against the PLL setting.
  795. * The agpgart_be code has not programmed the card yet,
  796. * so we can still tweak mode here.
  797. */
  798. agp_pll = io7->csrs->POx_RST[IO7_AGP_PORT].csr;
  799. switch(IO7_PLL_RNGB(agp_pll)) {
  800. case 0x4: /* 2x only */
  801. /*
  802. * The PLL is only programmed for 2x, so adjust the
  803. * rate to 2x, if necessary.
  804. */
  805. if (agp->mode.bits.rate != 2)
  806. new_rate = 2;
  807. break;
  808. case 0x6: /* 1x / 4x */
  809. /*
  810. * The PLL is programmed for 1x or 4x. Don't go faster
  811. * than requested, so if the requested rate is 2x, use 1x.
  812. */
  813. if (agp->mode.bits.rate == 2)
  814. new_rate = 1;
  815. break;
  816. default: /* ??????? */
  817. /*
  818. * Don't know what this PLL setting is, take the requested
  819. * rate, but warn the user.
  820. */
  821. printk("%s: unknown PLL setting RNGB=%lx (PLL6_CTL=%016lx)\n",
  822. __func__, IO7_PLL_RNGB(agp_pll), agp_pll);
  823. break;
  824. }
  825. /*
  826. * Set the new rate, if necessary.
  827. */
  828. if (new_rate) {
  829. printk("Requested AGP Rate %dX not compatible "
  830. "with PLL setting - using %dX\n",
  831. agp->mode.bits.rate,
  832. new_rate);
  833. agp->mode.bits.rate = new_rate;
  834. }
  835. printk("Enabling AGP on hose %d: %dX%s RQ %d\n",
  836. agp->hose->index, agp->mode.bits.rate,
  837. agp->mode.bits.sba ? " - SBA" : "", agp->mode.bits.rq);
  838. csrs->AGP_CMD.csr = agp->mode.lw;
  839. return 0;
  840. }
  841. static int
  842. marvel_agp_bind_memory(alpha_agp_info *agp, off_t pg_start, struct agp_memory *mem)
  843. {
  844. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  845. return iommu_bind(aper->arena, aper->pg_start + pg_start,
  846. mem->page_count, mem->pages);
  847. }
  848. static int
  849. marvel_agp_unbind_memory(alpha_agp_info *agp, off_t pg_start, struct agp_memory *mem)
  850. {
  851. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  852. return iommu_unbind(aper->arena, aper->pg_start + pg_start,
  853. mem->page_count);
  854. }
  855. static unsigned long
  856. marvel_agp_translate(alpha_agp_info *agp, dma_addr_t addr)
  857. {
  858. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  859. unsigned long baddr = addr - aper->arena->dma_base;
  860. unsigned long pte;
  861. if (addr < agp->aperture.bus_base ||
  862. addr >= agp->aperture.bus_base + agp->aperture.size) {
  863. printk("%s: addr out of range\n", __func__);
  864. return -EINVAL;
  865. }
  866. pte = aper->arena->ptes[baddr >> PAGE_SHIFT];
  867. if (!(pte & 1)) {
  868. printk("%s: pte not valid\n", __func__);
  869. return -EINVAL;
  870. }
  871. return (pte >> 1) << PAGE_SHIFT;
  872. }
  873. struct alpha_agp_ops marvel_agp_ops =
  874. {
  875. .setup = marvel_agp_setup,
  876. .cleanup = marvel_agp_cleanup,
  877. .configure = marvel_agp_configure,
  878. .bind = marvel_agp_bind_memory,
  879. .unbind = marvel_agp_unbind_memory,
  880. .translate = marvel_agp_translate
  881. };
  882. alpha_agp_info *
  883. marvel_agp_info(void)
  884. {
  885. struct pci_controller *hose;
  886. io7_ioport_csrs *csrs;
  887. alpha_agp_info *agp;
  888. struct io7 *io7;
  889. /*
  890. * Find the first IO7 with an AGP card.
  891. *
  892. * FIXME -- there should be a better way (we want to be able to
  893. * specify and what if the agp card is not video???)
  894. */
  895. hose = NULL;
  896. for (io7 = NULL; (io7 = marvel_next_io7(io7)) != NULL; ) {
  897. struct pci_controller *h;
  898. vuip addr;
  899. if (!io7->ports[IO7_AGP_PORT].enabled)
  900. continue;
  901. h = io7->ports[IO7_AGP_PORT].hose;
  902. addr = (vuip)build_conf_addr(h, 0, PCI_DEVFN(5, 0), 0);
  903. if (*addr != 0xffffffffu) {
  904. hose = h;
  905. break;
  906. }
  907. }
  908. if (!hose || !hose->sg_pci)
  909. return NULL;
  910. printk("MARVEL - using hose %d as AGP\n", hose->index);
  911. /*
  912. * Get the csrs from the hose.
  913. */
  914. csrs = ((struct io7_port *)hose->sysdata)->csrs;
  915. /*
  916. * Allocate the info structure.
  917. */
  918. agp = kmalloc(sizeof(*agp), GFP_KERNEL);
  919. if (!agp)
  920. return NULL;
  921. /*
  922. * Fill it in.
  923. */
  924. agp->hose = hose;
  925. agp->private = NULL;
  926. agp->ops = &marvel_agp_ops;
  927. /*
  928. * Aperture - not configured until ops.setup().
  929. */
  930. agp->aperture.bus_base = 0;
  931. agp->aperture.size = 0;
  932. agp->aperture.sysdata = NULL;
  933. /*
  934. * Capabilities.
  935. *
  936. * NOTE: IO7 reports through AGP_STAT that it can support a read queue
  937. * depth of 17 (rq = 0x10). It actually only supports a depth of
  938. * 16 (rq = 0xf).
  939. */
  940. agp->capability.lw = csrs->AGP_STAT.csr;
  941. agp->capability.bits.rq = 0xf;
  942. /*
  943. * Mode.
  944. */
  945. agp->mode.lw = csrs->AGP_CMD.csr;
  946. return agp;
  947. }