1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Init function</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="MTD NAND Driver Programming Interface"><link rel="up" href="basicboarddriver.html" title="Chapter&#160;4.&#160;Basic board driver"><link rel="prev" href="Device_ready_function.html" title="Device ready function"><link rel="next" href="Exit_function.html" title="Exit function"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Init function</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="Device_ready_function.html">Prev</a>&#160;</td><th width="60%" align="center">Chapter&#160;4.&#160;Basic board driver</th><td width="20%" align="right">&#160;<a accesskey="n" href="Exit_function.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="Init_function"></a>Init function</h2></div></div></div><p>
2			The init function allocates memory and sets up all the board
3			specific parameters and function pointers. When everything
4			is set up nand_scan() is called. This function tries to
5			detect and identify then chip. If a chip is found all the
6			internal data fields are initialized accordingly.
7			The structure(s) have to be zeroed out first and then filled with the necessary
8			information about the device.
9		</p><pre class="programlisting">
10static int __init board_init (void)
11{
12	struct nand_chip *this;
13	int err = 0;
14
15	/* Allocate memory for MTD device structure and private data */
16	board_mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
17	if (!board_mtd) {
18		printk ("Unable to allocate NAND MTD device structure.\n");
19		err = -ENOMEM;
20		goto out;
21	}
22
23	/* map physical address */
24	baseaddr = ioremap(CHIP_PHYSICAL_ADDRESS, 1024);
25	if (!baseaddr) {
26		printk("Ioremap to access NAND chip failed\n");
27		err = -EIO;
28		goto out_mtd;
29	}
30
31	/* Get pointer to private data */
32	this = (struct nand_chip *) ();
33	/* Link the private data with the MTD structure */
34	board_mtd-&gt;priv = this;
35
36	/* Set address of NAND IO lines */
37	this-&gt;IO_ADDR_R = baseaddr;
38	this-&gt;IO_ADDR_W = baseaddr;
39	/* Reference hardware control function */
40	this-&gt;hwcontrol = board_hwcontrol;
41	/* Set command delay time, see datasheet for correct value */
42	this-&gt;chip_delay = CHIP_DEPENDEND_COMMAND_DELAY;
43	/* Assign the device ready function, if available */
44	this-&gt;dev_ready = board_dev_ready;
45	this-&gt;eccmode = NAND_ECC_SOFT;
46
47	/* Scan to find existence of the device */
48	if (nand_scan (board_mtd, 1)) {
49		err = -ENXIO;
50		goto out_ior;
51	}
52	
53	add_mtd_partitions(board_mtd, partition_info, NUM_PARTITIONS);
54	goto out;
55
56out_ior:
57	iounmap(baseaddr);
58out_mtd:
59	kfree (board_mtd);
60out:
61	return err;
62}
63module_init(board_init);
64		</pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="Device_ready_function.html">Prev</a>&#160;</td><td width="20%" align="center"><a accesskey="u" href="basicboarddriver.html">Up</a></td><td width="40%" align="right">&#160;<a accesskey="n" href="Exit_function.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Device ready function&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Exit function</td></tr></table></div></body></html>
65