xref: /linux/arch/mips/txx9/generic/setup.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 /*
2  * Based on linux/arch/mips/txx9/rbtx4938/setup.c,
3  *	    and RBTX49xx patch from CELF patch archive.
4  *
5  * 2003-2005 (c) MontaVista Software, Inc.
6  * (C) Copyright TOSHIBA CORPORATION 2000-2001, 2004-2007
7  *
8  * This file is subject to the terms and conditions of the GNU General Public
9  * License.  See the file "COPYING" in the main directory of this archive
10  * for more details.
11  */
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/interrupt.h>
16 #include <linux/string.h>
17 #include <linux/export.h>
18 #include <linux/clk-provider.h>
19 #include <linux/clkdev.h>
20 #include <linux/err.h>
21 #include <linux/gpio/driver.h>
22 #include <linux/gpio/machine.h>
23 #include <linux/platform_device.h>
24 #include <linux/platform_data/txx9/ndfmc.h>
25 #include <linux/serial_core.h>
26 #include <linux/mtd/physmap.h>
27 #include <linux/leds.h>
28 #include <linux/device.h>
29 #include <linux/slab.h>
30 #include <linux/io.h>
31 #include <linux/irq.h>
32 #include <asm/bootinfo.h>
33 #include <asm/idle.h>
34 #include <asm/time.h>
35 #include <asm/reboot.h>
36 #include <asm/r4kcache.h>
37 #include <asm/setup.h>
38 #include <asm/txx9/generic.h>
39 #include <asm/txx9/pci.h>
40 #include <asm/txx9tmr.h>
41 #include <asm/txx9/dmac.h>
42 #ifdef CONFIG_CPU_TX49XX
43 #include <asm/txx9/tx4938.h>
44 #endif
45 
46 /* EBUSC settings of TX4927, etc. */
47 struct resource txx9_ce_res[8];
48 static char txx9_ce_res_name[8][4];	/* "CEn" */
49 
50 /* pcode, internal register */
51 unsigned int txx9_pcode;
52 char txx9_pcode_str[8];
53 static struct resource txx9_reg_res = {
54 	.name = txx9_pcode_str,
55 	.flags = IORESOURCE_MEM,
56 };
57 void __init
58 txx9_reg_res_init(unsigned int pcode, unsigned long base, unsigned long size)
59 {
60 	int i;
61 
62 	for (i = 0; i < ARRAY_SIZE(txx9_ce_res); i++) {
63 		sprintf(txx9_ce_res_name[i], "CE%d", i);
64 		txx9_ce_res[i].flags = IORESOURCE_MEM;
65 		txx9_ce_res[i].name = txx9_ce_res_name[i];
66 	}
67 
68 	txx9_pcode = pcode;
69 	sprintf(txx9_pcode_str, "TX%x", pcode);
70 	if (base) {
71 		txx9_reg_res.start = base & 0xfffffffffULL;
72 		txx9_reg_res.end = (base & 0xfffffffffULL) + (size - 1);
73 		request_resource(&iomem_resource, &txx9_reg_res);
74 	}
75 }
76 
77 /* clocks */
78 unsigned int txx9_master_clock;
79 unsigned int txx9_cpu_clock;
80 unsigned int txx9_gbus_clock;
81 
82 int txx9_ccfg_toeon __initdata = 1;
83 
84 #define BOARD_VEC(board)	extern struct txx9_board_vec board;
85 #include <asm/txx9/boards.h>
86 #undef BOARD_VEC
87 
88 struct txx9_board_vec *txx9_board_vec __initdata;
89 static char txx9_system_type[32];
90 
91 static struct txx9_board_vec *board_vecs[] __initdata = {
92 #define BOARD_VEC(board)	&board,
93 #include <asm/txx9/boards.h>
94 #undef BOARD_VEC
95 };
96 
97 static struct txx9_board_vec *__init find_board_byname(const char *name)
98 {
99 	int i;
100 
101 	/* search board_vecs table */
102 	for (i = 0; i < ARRAY_SIZE(board_vecs); i++) {
103 		if (strstr(board_vecs[i]->system, name))
104 			return board_vecs[i];
105 	}
106 	return NULL;
107 }
108 
109 static void __init prom_init_cmdline(void)
110 {
111 	int argc;
112 	int *argv32;
113 	int i;			/* Always ignore the "-c" at argv[0] */
114 
115 	if (fw_arg0 >= CKSEG0 || fw_arg1 < CKSEG0) {
116 		/*
117 		 * argc is not a valid number, or argv32 is not a valid
118 		 * pointer
119 		 */
120 		argc = 0;
121 		argv32 = NULL;
122 	} else {
123 		argc = (int)fw_arg0;
124 		argv32 = (int *)fw_arg1;
125 	}
126 
127 	arcs_cmdline[0] = '\0';
128 
129 	for (i = 1; i < argc; i++) {
130 		char *str = (char *)(long)argv32[i];
131 		if (i != 1)
132 			strcat(arcs_cmdline, " ");
133 		if (strchr(str, ' ')) {
134 			strcat(arcs_cmdline, "\"");
135 			strcat(arcs_cmdline, str);
136 			strcat(arcs_cmdline, "\"");
137 		} else
138 			strcat(arcs_cmdline, str);
139 	}
140 }
141 
142 static int txx9_ic_disable __initdata;
143 static int txx9_dc_disable __initdata;
144 
145 #if defined(CONFIG_CPU_TX49XX)
146 /* flush all cache on very early stage (before 4k_cache_init) */
147 static void __init early_flush_dcache(void)
148 {
149 	unsigned int conf = read_c0_config();
150 	unsigned int dc_size = 1 << (12 + ((conf & CONF_DC) >> 6));
151 	unsigned int linesz = 32;
152 	unsigned long addr, end;
153 
154 	end = INDEX_BASE + dc_size / 4;
155 	/* 4way, waybit=0 */
156 	for (addr = INDEX_BASE; addr < end; addr += linesz) {
157 		cache_op(Index_Writeback_Inv_D, addr | 0);
158 		cache_op(Index_Writeback_Inv_D, addr | 1);
159 		cache_op(Index_Writeback_Inv_D, addr | 2);
160 		cache_op(Index_Writeback_Inv_D, addr | 3);
161 	}
162 }
163 
164 static void __init txx9_cache_fixup(void)
165 {
166 	unsigned int conf;
167 
168 	conf = read_c0_config();
169 	/* flush and disable */
170 	if (txx9_ic_disable) {
171 		conf |= TX49_CONF_IC;
172 		write_c0_config(conf);
173 	}
174 	if (txx9_dc_disable) {
175 		early_flush_dcache();
176 		conf |= TX49_CONF_DC;
177 		write_c0_config(conf);
178 	}
179 
180 	/* enable cache */
181 	conf = read_c0_config();
182 	if (!txx9_ic_disable)
183 		conf &= ~TX49_CONF_IC;
184 	if (!txx9_dc_disable)
185 		conf &= ~TX49_CONF_DC;
186 	write_c0_config(conf);
187 
188 	if (conf & TX49_CONF_IC)
189 		pr_info("TX49XX I-Cache disabled.\n");
190 	if (conf & TX49_CONF_DC)
191 		pr_info("TX49XX D-Cache disabled.\n");
192 }
193 #else
194 static inline void txx9_cache_fixup(void)
195 {
196 }
197 #endif
198 
199 static void __init preprocess_cmdline(void)
200 {
201 	static char cmdline[COMMAND_LINE_SIZE] __initdata;
202 	char *s;
203 
204 	strscpy(cmdline, arcs_cmdline);
205 	s = cmdline;
206 	arcs_cmdline[0] = '\0';
207 	while (s && *s) {
208 		char *str = strsep(&s, " ");
209 		if (strncmp(str, "board=", 6) == 0) {
210 			txx9_board_vec = find_board_byname(str + 6);
211 			continue;
212 		} else if (strncmp(str, "masterclk=", 10) == 0) {
213 			unsigned int val;
214 			if (kstrtouint(str + 10, 10, &val) == 0)
215 				txx9_master_clock = val;
216 			continue;
217 		} else if (strcmp(str, "icdisable") == 0) {
218 			txx9_ic_disable = 1;
219 			continue;
220 		} else if (strcmp(str, "dcdisable") == 0) {
221 			txx9_dc_disable = 1;
222 			continue;
223 		} else if (strcmp(str, "toeoff") == 0) {
224 			txx9_ccfg_toeon = 0;
225 			continue;
226 		} else if (strcmp(str, "toeon") == 0) {
227 			txx9_ccfg_toeon = 1;
228 			continue;
229 		}
230 		if (arcs_cmdline[0])
231 			strcat(arcs_cmdline, " ");
232 		strcat(arcs_cmdline, str);
233 	}
234 
235 	txx9_cache_fixup();
236 }
237 
238 static void __init select_board(void)
239 {
240 	const char *envstr;
241 
242 	/* first, determine by "board=" argument in preprocess_cmdline() */
243 	if (txx9_board_vec)
244 		return;
245 	/* next, determine by "board" envvar */
246 	envstr = prom_getenv("board");
247 	if (envstr) {
248 		txx9_board_vec = find_board_byname(envstr);
249 		if (txx9_board_vec)
250 			return;
251 	}
252 
253 	/* select "default" board */
254 #ifdef CONFIG_CPU_TX49XX
255 	switch (TX4938_REV_PCODE()) {
256 #ifdef CONFIG_TOSHIBA_RBTX4927
257 	case 0x4927:
258 		txx9_board_vec = &rbtx4927_vec;
259 		break;
260 	case 0x4937:
261 		txx9_board_vec = &rbtx4937_vec;
262 		break;
263 #endif
264 	}
265 #endif
266 }
267 
268 void __init prom_init(void)
269 {
270 	prom_init_cmdline();
271 	preprocess_cmdline();
272 	select_board();
273 
274 	strscpy(txx9_system_type, txx9_board_vec->system);
275 
276 	txx9_board_vec->prom_init();
277 }
278 
279 const char *get_system_type(void)
280 {
281 	return txx9_system_type;
282 }
283 
284 const char *__init prom_getenv(const char *name)
285 {
286 	const s32 *str;
287 
288 	if (fw_arg2 < CKSEG0)
289 		return NULL;
290 
291 	str = (const s32 *)fw_arg2;
292 	/* YAMON style ("name", "value" pairs) */
293 	while (str[0] && str[1]) {
294 		if (!strcmp((const char *)(unsigned long)str[0], name))
295 			return (const char *)(unsigned long)str[1];
296 		str += 2;
297 	}
298 	return NULL;
299 }
300 
301 static void __noreturn txx9_machine_halt(void)
302 {
303 	local_irq_disable();
304 	clear_c0_status(ST0_IM);
305 	while (1) {
306 		if (cpu_wait) {
307 			(*cpu_wait)();
308 			if (cpu_has_counter) {
309 				/*
310 				 * Clear counter interrupt while it
311 				 * breaks WAIT instruction even if
312 				 * masked.
313 				 */
314 				write_c0_compare(0);
315 			}
316 		}
317 	}
318 }
319 
320 /* Watchdog support */
321 void __init txx9_wdt_init(unsigned long base)
322 {
323 	struct resource res = {
324 		.start	= base,
325 		.end	= base + 0x100 - 1,
326 		.flags	= IORESOURCE_MEM,
327 	};
328 	platform_device_register_simple("txx9wdt", -1, &res, 1);
329 }
330 
331 void txx9_wdt_now(unsigned long base)
332 {
333 	struct txx9_tmr_reg __iomem *tmrptr =
334 		ioremap(base, sizeof(struct txx9_tmr_reg));
335 	/* disable watch dog timer */
336 	__raw_writel(TXx9_TMWTMR_WDIS | TXx9_TMWTMR_TWC, &tmrptr->wtmr);
337 	__raw_writel(0, &tmrptr->tcr);
338 	/* kick watchdog */
339 	__raw_writel(TXx9_TMWTMR_TWIE, &tmrptr->wtmr);
340 	__raw_writel(1, &tmrptr->cpra); /* immediate */
341 	__raw_writel(TXx9_TMTCR_TCE | TXx9_TMTCR_CCDE | TXx9_TMTCR_TMODE_WDOG,
342 		     &tmrptr->tcr);
343 }
344 
345 void __init txx9_ethaddr_init(unsigned int id, unsigned char *ethaddr)
346 {
347 	struct platform_device *pdev =
348 		platform_device_alloc("tc35815-mac", id);
349 	if (!pdev ||
350 	    platform_device_add_data(pdev, ethaddr, 6) ||
351 	    platform_device_add(pdev))
352 		platform_device_put(pdev);
353 }
354 
355 void __init txx9_sio_init(unsigned long baseaddr, int irq,
356 			  unsigned int line, unsigned int sclk, int nocts)
357 {
358 #ifdef CONFIG_SERIAL_TXX9
359 	struct uart_port req;
360 
361 	memset(&req, 0, sizeof(req));
362 	req.line = line;
363 	req.iotype = UPIO_MEM;
364 	req.membase = ioremap(baseaddr, 0x24);
365 	req.mapbase = baseaddr;
366 	req.irq = irq;
367 	if (!nocts)
368 		req.flags |= UPF_BUGGY_UART /*HAVE_CTS_LINE*/;
369 	if (sclk) {
370 		req.flags |= UPF_MAGIC_MULTIPLIER /*USE_SCLK*/;
371 		req.uartclk = sclk;
372 	} else
373 		req.uartclk = TXX9_IMCLK;
374 	early_serial_txx9_setup(&req);
375 #endif /* CONFIG_SERIAL_TXX9 */
376 }
377 
378 #ifdef CONFIG_EARLY_PRINTK
379 static void null_prom_putchar(char c)
380 {
381 }
382 void (*txx9_prom_putchar)(char c) = null_prom_putchar;
383 
384 void prom_putchar(char c)
385 {
386 	txx9_prom_putchar(c);
387 }
388 
389 static void __iomem *early_txx9_sio_port;
390 
391 static void early_txx9_sio_putchar(char c)
392 {
393 #define TXX9_SICISR	0x0c
394 #define TXX9_SITFIFO	0x1c
395 #define TXX9_SICISR_TXALS	0x00000002
396 	while (!(__raw_readl(early_txx9_sio_port + TXX9_SICISR) &
397 		 TXX9_SICISR_TXALS))
398 		;
399 	__raw_writel(c, early_txx9_sio_port + TXX9_SITFIFO);
400 }
401 
402 void __init txx9_sio_putchar_init(unsigned long baseaddr)
403 {
404 	early_txx9_sio_port = ioremap(baseaddr, 0x24);
405 	txx9_prom_putchar = early_txx9_sio_putchar;
406 }
407 #endif /* CONFIG_EARLY_PRINTK */
408 
409 /* wrappers */
410 void __init plat_mem_setup(void)
411 {
412 	ioport_resource.start = 0;
413 	ioport_resource.end = ~0UL;	/* no limit */
414 	iomem_resource.start = 0;
415 	iomem_resource.end = ~0UL;	/* no limit */
416 
417 	/* fallback restart/halt routines */
418 	_machine_restart = (void (*)(char *))txx9_machine_halt;
419 	_machine_halt = txx9_machine_halt;
420 	pm_power_off = txx9_machine_halt;
421 
422 #ifdef CONFIG_PCI
423 	pcibios_plat_setup = txx9_pcibios_setup;
424 #endif
425 	txx9_board_vec->mem_setup();
426 }
427 
428 void __init arch_init_irq(void)
429 {
430 	txx9_board_vec->irq_setup();
431 }
432 
433 void __init plat_time_init(void)
434 {
435 #ifdef CONFIG_CPU_TX49XX
436 	mips_hpt_frequency = txx9_cpu_clock / 2;
437 #endif
438 	txx9_board_vec->time_init();
439 }
440 
441 static void txx9_clk_init(void)
442 {
443 	struct clk_hw *hw;
444 	int error;
445 
446 	hw = clk_hw_register_fixed_rate(NULL, "gbus", NULL, 0, txx9_gbus_clock);
447 	if (IS_ERR(hw)) {
448 		error = PTR_ERR(hw);
449 		goto fail;
450 	}
451 
452 	hw = clk_hw_register_fixed_factor(NULL, "imbus", "gbus", 0, 1, 2);
453 	error = clk_hw_register_clkdev(hw, "imbus_clk", NULL);
454 	if (error)
455 		goto fail;
456 
457 #ifdef CONFIG_CPU_TX49XX
458 	if (TX4938_REV_PCODE() == 0x4938) {
459 		hw = clk_hw_register_fixed_factor(NULL, "spi", "gbus", 0, 1, 4);
460 		error = clk_hw_register_clkdev(hw, "spi-baseclk", NULL);
461 		if (error)
462 			goto fail;
463 	}
464 #endif
465 
466 	return;
467 
468 fail:
469 	pr_err("Failed to register clocks: %d\n", error);
470 }
471 
472 static int __init _txx9_arch_init(void)
473 {
474 	txx9_clk_init();
475 
476 	if (txx9_board_vec->arch_init)
477 		txx9_board_vec->arch_init();
478 	return 0;
479 }
480 arch_initcall(_txx9_arch_init);
481 
482 static int __init _txx9_device_init(void)
483 {
484 	if (txx9_board_vec->device_init)
485 		txx9_board_vec->device_init();
486 	return 0;
487 }
488 device_initcall(_txx9_device_init);
489 
490 int (*txx9_irq_dispatch)(int pending);
491 asmlinkage void plat_irq_dispatch(void)
492 {
493 	int pending = read_c0_status() & read_c0_cause() & ST0_IM;
494 	int irq = txx9_irq_dispatch(pending);
495 
496 	if (likely(irq >= 0))
497 		do_IRQ(irq);
498 	else
499 		spurious_interrupt();
500 }
501 
502 /* see include/asm-mips/mach-tx39xx/mangle-port.h, for example. */
503 #ifdef NEEDS_TXX9_SWIZZLE_ADDR_B
504 static unsigned long __swizzle_addr_none(unsigned long port)
505 {
506 	return port;
507 }
508 unsigned long (*__swizzle_addr_b)(unsigned long port) = __swizzle_addr_none;
509 EXPORT_SYMBOL(__swizzle_addr_b);
510 #endif
511 
512 void __init txx9_physmap_flash_init(int no, unsigned long addr,
513 				    unsigned long size,
514 				    const struct physmap_flash_data *pdata)
515 {
516 #if IS_ENABLED(CONFIG_MTD_PHYSMAP)
517 	struct resource res = {
518 		.start = addr,
519 		.end = addr + size - 1,
520 		.flags = IORESOURCE_MEM,
521 	};
522 	struct platform_device *pdev;
523 	static struct mtd_partition parts[2];
524 	struct physmap_flash_data pdata_part;
525 
526 	/* If this area contained boot area, make separate partition */
527 	if (pdata->nr_parts == 0 && !pdata->parts &&
528 	    addr < 0x1fc00000 && addr + size > 0x1fc00000 &&
529 	    !parts[0].name) {
530 		parts[0].name = "boot";
531 		parts[0].offset = 0x1fc00000 - addr;
532 		parts[0].size = addr + size - 0x1fc00000;
533 		parts[1].name = "user";
534 		parts[1].offset = 0;
535 		parts[1].size = 0x1fc00000 - addr;
536 		pdata_part = *pdata;
537 		pdata_part.nr_parts = ARRAY_SIZE(parts);
538 		pdata_part.parts = parts;
539 		pdata = &pdata_part;
540 	}
541 
542 	pdev = platform_device_alloc("physmap-flash", no);
543 	if (!pdev ||
544 	    platform_device_add_resources(pdev, &res, 1) ||
545 	    platform_device_add_data(pdev, pdata, sizeof(*pdata)) ||
546 	    platform_device_add(pdev))
547 		platform_device_put(pdev);
548 #endif
549 }
550 
551 void __init txx9_ndfmc_init(unsigned long baseaddr,
552 			    const struct txx9ndfmc_platform_data *pdata)
553 {
554 #if IS_ENABLED(CONFIG_MTD_NAND_TXX9NDFMC)
555 	struct resource res = {
556 		.start = baseaddr,
557 		.end = baseaddr + 0x1000 - 1,
558 		.flags = IORESOURCE_MEM,
559 	};
560 	struct platform_device *pdev = platform_device_alloc("txx9ndfmc", -1);
561 
562 	if (!pdev ||
563 	    platform_device_add_resources(pdev, &res, 1) ||
564 	    platform_device_add_data(pdev, pdata, sizeof(*pdata)) ||
565 	    platform_device_add(pdev))
566 		platform_device_put(pdev);
567 #endif
568 }
569 
570 #if IS_ENABLED(CONFIG_LEDS_GPIO)
571 static DEFINE_SPINLOCK(txx9_iocled_lock);
572 
573 #define TXX9_IOCLED_MAXLEDS 3	/* rbtx4927 */
574 
575 struct txx9_iocled_data {
576 	struct gpio_chip chip;
577 	u8 cur_val;
578 	void __iomem *mmioaddr;
579 	struct gpio_led_platform_data pdata;
580 	struct gpio_led leds[TXX9_IOCLED_MAXLEDS];
581 	char names[TXX9_IOCLED_MAXLEDS][32];
582 };
583 
584 static int txx9_iocled_get(struct gpio_chip *chip, unsigned int offset)
585 {
586 	struct txx9_iocled_data *data = gpiochip_get_data(chip);
587 	return !!(data->cur_val & (1 << offset));
588 }
589 
590 static int txx9_iocled_set(struct gpio_chip *chip, unsigned int offset,
591 			   int value)
592 {
593 	struct txx9_iocled_data *data = gpiochip_get_data(chip);
594 	unsigned long flags;
595 	spin_lock_irqsave(&txx9_iocled_lock, flags);
596 	if (value)
597 		data->cur_val |= 1 << offset;
598 	else
599 		data->cur_val &= ~(1 << offset);
600 	writeb(data->cur_val, data->mmioaddr);
601 	mmiowb();
602 	spin_unlock_irqrestore(&txx9_iocled_lock, flags);
603 
604 	return 0;
605 }
606 
607 static int txx9_iocled_dir_in(struct gpio_chip *chip, unsigned int offset)
608 {
609 	return 0;
610 }
611 
612 static int txx9_iocled_dir_out(struct gpio_chip *chip, unsigned int offset,
613 			       int value)
614 {
615 	txx9_iocled_set(chip, offset, value);
616 	return 0;
617 }
618 
619 static struct gpiod_lookup_table txx9_iocled_table = {
620 	.table = {
621 		GPIO_LOOKUP_IDX("iocled", 0, NULL, 0, GPIO_ACTIVE_LOW),
622 		GPIO_LOOKUP_IDX("iocled", 1, NULL, 1, GPIO_ACTIVE_LOW),
623 		GPIO_LOOKUP_IDX("iocled", 2, NULL, 2, GPIO_ACTIVE_LOW),
624 		{ },
625 	},
626 };
627 
628 void __init txx9_iocled_init(unsigned long baseaddr, unsigned int num,
629 			     const char *color, char **deftriggers)
630 {
631 	struct txx9_iocled_data *iocled;
632 	struct platform_device *pdev;
633 	int i;
634 	static char *default_triggers[] __initdata = {
635 		"heartbeat",
636 		"disk-activity",
637 		"nand-disk",
638 		NULL,
639 	};
640 
641 	if (!deftriggers)
642 		deftriggers = default_triggers;
643 	iocled = kzalloc_obj(*iocled);
644 	if (!iocled)
645 		return;
646 	iocled->mmioaddr = ioremap(baseaddr, 1);
647 	if (!iocled->mmioaddr)
648 		goto out_free;
649 	iocled->chip.get = txx9_iocled_get;
650 	iocled->chip.set = txx9_iocled_set;
651 	iocled->chip.direction_input = txx9_iocled_dir_in;
652 	iocled->chip.direction_output = txx9_iocled_dir_out;
653 	iocled->chip.label = "iocled";
654 	iocled->chip.base = -1;
655 	iocled->chip.ngpio = num;
656 	if (gpiochip_add_data(&iocled->chip, iocled))
657 		goto out_unmap;
658 
659 	pdev = platform_device_alloc("leds-gpio", iocled->chip.base);
660 	if (!pdev)
661 		goto out_gpio;
662 	iocled->pdata.num_leds = num;
663 	iocled->pdata.leds = iocled->leds;
664 	for (i = 0; i < num; i++) {
665 		struct gpio_led *led = &iocled->leds[i];
666 		snprintf(iocled->names[i], sizeof(iocled->names[i]),
667 			 "iocled:%s:%u", color, i);
668 		led->name = iocled->names[i];
669 		if (deftriggers && *deftriggers)
670 			led->default_trigger = *deftriggers++;
671 	}
672 	pdev->dev.platform_data = &iocled->pdata;
673 	if (platform_device_add(pdev))
674 		goto out_pdev;
675 	txx9_iocled_table.dev_id = dev_name(&pdev->dev);
676 	gpiod_add_lookup_table(&txx9_iocled_table);
677 	return;
678 
679 out_pdev:
680 	platform_device_put(pdev);
681 out_gpio:
682 	gpiochip_remove(&iocled->chip);
683 out_unmap:
684 	iounmap(iocled->mmioaddr);
685 out_free:
686 	kfree(iocled);
687 }
688 #else /* CONFIG_LEDS_GPIO */
689 void __init txx9_iocled_init(unsigned long baseaddr, unsigned int num,
690 			     const char *color, char **deftriggers)
691 {
692 }
693 #endif /* CONFIG_LEDS_GPIO */
694 
695 void __init txx9_dmac_init(int id, unsigned long baseaddr, int irq,
696 			   const struct txx9dmac_platform_data *pdata)
697 {
698 #if IS_ENABLED(CONFIG_TXX9_DMAC)
699 	struct resource res[] = {
700 		{
701 			.start = baseaddr,
702 			.end = baseaddr + 0x800 - 1,
703 			.flags = IORESOURCE_MEM,
704 #ifndef CONFIG_MACH_TX49XX
705 		}, {
706 			.start = irq,
707 			.flags = IORESOURCE_IRQ,
708 #endif
709 		}
710 	};
711 #ifdef CONFIG_MACH_TX49XX
712 	struct resource chan_res[] = {
713 		{
714 			.flags = IORESOURCE_IRQ,
715 		}
716 	};
717 #endif
718 	struct platform_device *pdev = platform_device_alloc("txx9dmac", id);
719 	struct txx9dmac_chan_platform_data cpdata;
720 	int i;
721 
722 	if (!pdev ||
723 	    platform_device_add_resources(pdev, res, ARRAY_SIZE(res)) ||
724 	    platform_device_add_data(pdev, pdata, sizeof(*pdata)) ||
725 	    platform_device_add(pdev)) {
726 		platform_device_put(pdev);
727 		return;
728 	}
729 	memset(&cpdata, 0, sizeof(cpdata));
730 	cpdata.dmac_dev = pdev;
731 	for (i = 0; i < TXX9_DMA_MAX_NR_CHANNELS; i++) {
732 #ifdef CONFIG_MACH_TX49XX
733 		chan_res[0].start = irq + i;
734 #endif
735 		pdev = platform_device_alloc("txx9dmac-chan",
736 					     id * TXX9_DMA_MAX_NR_CHANNELS + i);
737 		if (!pdev ||
738 #ifdef CONFIG_MACH_TX49XX
739 		    platform_device_add_resources(pdev, chan_res,
740 						  ARRAY_SIZE(chan_res)) ||
741 #endif
742 		    platform_device_add_data(pdev, &cpdata, sizeof(cpdata)) ||
743 		    platform_device_add(pdev))
744 			platform_device_put(pdev);
745 	}
746 #endif
747 }
748 
749 void __init txx9_aclc_init(unsigned long baseaddr, int irq,
750 			   unsigned int dmac_id,
751 			   unsigned int dma_chan_out,
752 			   unsigned int dma_chan_in)
753 {
754 }
755 
756 static const struct bus_type txx9_sramc_subsys = {
757 	.name = "txx9_sram",
758 	.dev_name = "txx9_sram",
759 };
760 
761 struct txx9_sramc_dev {
762 	struct device dev;
763 	struct bin_attribute bindata_attr;
764 	void __iomem *base;
765 };
766 
767 static ssize_t txx9_sram_read(struct file *filp, struct kobject *kobj,
768 			      const struct bin_attribute *bin_attr,
769 			      char *buf, loff_t pos, size_t size)
770 {
771 	struct txx9_sramc_dev *dev = bin_attr->private;
772 	size_t ramsize = bin_attr->size;
773 
774 	if (pos >= ramsize)
775 		return 0;
776 	if (pos + size > ramsize)
777 		size = ramsize - pos;
778 	memcpy_fromio(buf, dev->base + pos, size);
779 	return size;
780 }
781 
782 static ssize_t txx9_sram_write(struct file *filp, struct kobject *kobj,
783 			       const struct bin_attribute *bin_attr,
784 			       char *buf, loff_t pos, size_t size)
785 {
786 	struct txx9_sramc_dev *dev = bin_attr->private;
787 	size_t ramsize = bin_attr->size;
788 
789 	if (pos >= ramsize)
790 		return 0;
791 	if (pos + size > ramsize)
792 		size = ramsize - pos;
793 	memcpy_toio(dev->base + pos, buf, size);
794 	return size;
795 }
796 
797 static void txx9_device_release(struct device *dev)
798 {
799 	struct txx9_sramc_dev *tdev;
800 
801 	tdev = container_of(dev, struct txx9_sramc_dev, dev);
802 	kfree(tdev);
803 }
804 
805 void __init txx9_sramc_init(struct resource *r)
806 {
807 	struct txx9_sramc_dev *dev;
808 	size_t size;
809 	int err;
810 
811 	err = subsys_system_register(&txx9_sramc_subsys, NULL);
812 	if (err)
813 		return;
814 	dev = kzalloc_obj(*dev);
815 	if (!dev)
816 		return;
817 	size = resource_size(r);
818 	dev->base = ioremap(r->start, size);
819 	if (!dev->base) {
820 		kfree(dev);
821 		return;
822 	}
823 	dev->dev.release = &txx9_device_release;
824 	dev->dev.bus = &txx9_sramc_subsys;
825 	sysfs_bin_attr_init(&dev->bindata_attr);
826 	dev->bindata_attr.attr.name = "bindata";
827 	dev->bindata_attr.attr.mode = S_IRUSR | S_IWUSR;
828 	dev->bindata_attr.read = txx9_sram_read;
829 	dev->bindata_attr.write = txx9_sram_write;
830 	dev->bindata_attr.size = size;
831 	dev->bindata_attr.private = dev;
832 	err = device_register(&dev->dev);
833 	if (err)
834 		goto exit_put;
835 	err = sysfs_create_bin_file(&dev->dev.kobj, &dev->bindata_attr);
836 	if (err) {
837 		iounmap(dev->base);
838 		device_unregister(&dev->dev);
839 	}
840 	return;
841 exit_put:
842 	iounmap(dev->base);
843 	put_device(&dev->dev);
844 }
845